def test_sleep(self):
     """
     Tests that the sleep command works properly.
     """
     
     # just test if it runs
     ergo("sleep 13")
    def test_size(self):
        """
        Tests the size command.
        """

        # create a file of a random size
        randsize = random.randint(1, 100000)

        # create the file
        with open("test_size", "wb") as f:
            f.seek(randsize)
            f.write(b"testing")

        # ensure the size is correct in bytes and kilobytes
        self.assertEqual(
            ergo("size -u byte test_size"), "test_size: " +
            str(float(file_or_dir_size("test_size"))) + " byte(s)")

        self.assertEqual(
            ergo("size -u B test_size"), "test_size: " +
            str(float(file_or_dir_size("test_size"))) + " byte(s)")

        self.assertEqual(
            ergo("size test_size"), "test_size: " +
            str(float(file_or_dir_size("test_size")) / 1024.0) +
            " kilobyte(s)")

        os.remove("test_size")
Exemple #3
0
 def test_get_set(self):
     """
     Tests the get and set commands.
     """
     ergo("set {lol_this_is_a_random_variable_name:1337}")
     self.assertEqual(ergo("get lol_this_is_a_random_variable_name"),
                      [1337])
    def test_cow(self):
        """
        Tests the cow command.
        """

        self.assertEqual(makevalidcow("123"), ergo("cow 123"))
        self.assertEqual(makevalidcow("abcabcabc"), ergo("cow abcabcabc"))
    def test_write(self):
        """
        Tests the write command.
        """

        # first write to the file
        ergo(
            "print oq4ij 4ojioj1iorj oo4joijoi12 | write -a test_write_file {0}"
        )

        # then check that the contents are correct
        with open("test_write_file") as f:
            self.assertEqual(f.read(), "oq4ij\n4ojioj1iorj\noo4joijoi12\n")

        # then cleanup
        os.remove("test_write_file")

        # first write to the file
        ergo("print oq4ij 4ojioj1iorj oo4joijoi12 | write test_write_file {0}")

        # then check that the contents are correct
        with open("test_write_file") as f:
            self.assertEqual(f.read(), "oo4joijoi12\n")

        # then cleanup
        os.remove("test_write_file")
Exemple #6
0
    def test_alias(self):
        """
        Test the alias command.
        """
        ergo('alias LIST_THEM_FILES ls')

        self.assertEqual(ergo("LIST_THEM_FILES"), ergo("ls"))
    def test_swap(self):
        """
        Tests the swap command.
        """

        with open("test_swap_file", "w") as f:
            f.write("example content 1")

        with open("test_swap_file2", "w") as f:
            f.write("example content 2")

        # assert that contents are in the right files
        with open("test_swap_file") as f:
            self.assertEqual(f.read(), "example content 1")

        with open("test_swap_file2") as f:
            self.assertEqual(f.read(), "example content 2")

        # swap them
        ergo("swap test_swap_file test_swap_file2")

        # assert that contents are switche
        with open("test_swap_file") as f:
            self.assertEqual(f.read(), "example content 2")

        with open("test_swap_file2") as f:
            self.assertEqual(f.read(), "example content 1")

        # cleanup
        os.remove("test_swap_file")
        os.remove("test_swap_file2")
 def test_add(self):
     """
     Tests that + (the addition command) works properly.
     """
     
     self.assertEqual(ergo("+ 3 3 3", 9))
     self.assertCountEqual(ergo("+ (list 1 3 5) (list 2 4 6)"), [1, 2, 3, 4, 5, 6])
     self.assertEqual(ergo("+ stringa stringb"), "stringastringb")
Exemple #9
0
    def test_ping(self):
        """
        Tests the ping command.
        """

        ergo("ping aaaaaaaaaaaaaaa")  # will say its down
        # (won't throw exception)
        ergo("ping -c 2 8.8.8.8 google.com")
    def test_mkdir(self):
        """
        Tests the mkdir command.
        """

        self.assertFalse(os.path.isdir("test_mkdir"))
        ergo("mkdir test_mkdir")
        self.assertTrue(os.path.isdir("test_mkdir"))
        os.rmdir("test_mkdir")
    def test_sysinfo_dyn(self):
        """
        Tests sysinfo on dynamic attributes (sysinfo dyn).
        """

        # cpu percent usage will likely change by the time second operation is done.
        # this simply tests that `sysinfo dyn -p` won't throw an error

        ergo("sysinfo dyn -u")
    def test_temp(self):
        """
        Tests the temp command.
        """

        # ensure that the filename is not taken
        self.assertFalse(os.path.isfile(ergo("temp file")))

        # ensure that the tempdir was successfully created
        self.assertTrue(os.path.isdir(ergo("temp dir")))
Exemple #13
0
    def test_environment(self):
        """
        Tests the environment command.
        """

        # set a variable
        ergo("environment set prompt 123123123123> ")

        # verify its value
        self.assertEqual(ergo("environment get prompt"), "123123123123>")
Exemple #14
0
    def test_lonely_operator(self):
        """
        "Lonely (valid) operators returning OperatorError"
        https://github.com/ergonomica/ergonomica/issues/16
        """

        try:
            ergo("(map)")
        except Exception as error:
            self.assertNotEqual(
                str(error), ["[ergo: OperatorError]: No such operator 'map'."])
Exemple #15
0
    def test_download(self):
        """
        Tests the download command.
        """

        ergo("download http://ergonomica.github.io/test-download")

        with open("test-download") as f:
            self.assertEqual(f.read(), "this\nis\na\ntest!\n")

        os.remove("test-download")
Exemple #16
0
 def test_addline(self):
     """
     Test the addline command.
     """
     open("test-addline", "w")
     ergo(
         'addline "TESTING this feature\n" "once again testing this feature" {file:"test-addline"}'
     )
     self.assertEqual(
         open("test-addline", "r").readlines(),
         ["TESTING this feature\n", "once again testing this feature"])
Exemple #17
0
 def test_cp(self):
     """
     Test the cp command.
     """
     try:
         ergo("rm test-cp-2")
     except:
         pass
     open("test-cp-1", "w")
     ergo("cp test-cp-1 test-cp-2")
     self.assert_("test-cp-2" in os.listdir("."))
    def test_pyvim(self):
        """
        Tests the pyvim command.
        """

        try:
            ergo("pyvim")
        except Exception as error:
            if error.args[0] == "[ergo]: [pyvim: PlatformError]: Pyvim not supported on this system.":
                pass
            else:
                raise error
Exemple #19
0
    def test_cd(self):
        """
        Test the cd command.
        """

        original = ergo("pwd")
        try:
            os.mkdir("test-cd")
        except OSError:
            # directory already exists
            pass
        ergo("cd test-cd")
        self.assertEqual(original + "/test-cd", ergo("pwd"))
Exemple #20
0
    def test_del_variable(self):
        """
        Tests the del function on a user-defined variable.
        """

        # create the variable
        ergo("set x 2")

        # delete it
        ergo("del x")

        # check that it's not still in the namespace
        self.assertEqual(ergo("print $x"), None)
Exemple #21
0
    def test_py(self):
        """
        Tests the py command.
        """

        # set a variable in the Ergonomica namespace
        ergo("set l 2")

        # use it in a Python expression
        self.assertEqual(ergo("py \"l + 2\""), 4)

        # delete the variable after use
        ergo("del l")
Exemple #22
0
    def test_cd(self):
        """
        Tests the cd command.
        """

        olddir = os.getcwd()
        newdir = tempfile.mkdtemp()

        ergo("cd {}".format(newdir))

        self.assertEqual(os.getcwd(), newdir)

        ergo("cd {}".format(olddir))

        self.assertEqual(os.getcwd(), olddir)
Exemple #23
0
    def test_time(self):
        """
        Test the time function.
        """

        self.assertEqual(ergo('time {}'.format(STRFTIME_TEST_STRING)),
                         strftime(STRFTIME_TEST_STRING, gmtime()))
Exemple #24
0
    def test_net_globalip(self):
        """
        Tests the net command getting the global IP.
        """

        self.assertEqual(ergo("net ip global"),
                              requests.get("http://ip.42.pl/raw").text)
 def test_nested_functions(self):
     """
     Test that functions defined inside other functions behave correctly.
     """
     self.assertEqual(
         ergo("def f\n    def g\n        print testing\n   g\n"),
         [["testing"]])
    def test_range_start_end_step(self):
        """
        Test a range with start, end, and step specified.
        """

        self.assertCountEqual(ergo("range 1 10 1"),
                              [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
    def test_single_print(self):
        """
        Test printing a single string.
        """

        self.assertEqual(ergo('print "this is an example of a \'string\'"'),
                         "this is an example of a 'string'")
Exemple #28
0
    def test_list_modules(self):
        """
        Tests the list_modules command.
        """

        # old (confirmed working) implementation. test that any new implementation will match.
        files = os.listdir(os.path.join(os.path.join(os.path.expanduser("~"), ".ergo"), "packages"))
        self.assertEqual(ergo('list_modules'), [f.replace(".py", "") for f in files if f.endswith(".py") and f != "__init__.py"])
    def test_read(self):
        """
        Tests the read command.
        """

        # create the files for testing
        with open("test_read_1", "w") as f, open("test_read_2", "w") as g:
            f.write("a\nb\nc")
            g.write("1\n2\n3")

        # assert that they match what Ergonomica reads
        self.assertEqual(ergo("read test_read_1"), ["a", "b", "c"])
        self.assertEqual(ergo("read test_read_2"), ["1", "2", "3"])

        # delete them
        os.remove("test_read_1")
        os.remove("test_read_2")
    def test_range_start_end_step_undershoot(self):
        """
        Test a range in which (end - start) is not divisible by step.
        """

        self.assertCountEqual(
            ergo("range 1 10 1.7"),
            [1.0, 2.7, 4.4, 6.1000000000000005, 7.800000000000001, 9.5])