コード例 #1
0
    def test_assign_dict(self):
        """Tests if D2TXT accepts assignment using dictionaries."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        d2txt.append({})
        d2txt.extend([
            {
                "column 1": "foo",
                "column 2": "bar"
            },
            {
                "column 1": "1",
                "column 2": "2",
                "column 3": "3",
                "column 4": "ignored",
            },
        ])
        d2txt.insert(1, {"column 1": "a", "column 2": "b", "column 3": "c"})

        self.assertEqual(len(d2txt), 4)
        for i, row in enumerate(d2txt):
            with self.subTest(i=i):
                self.assertEqual(len(row), 3)

        self.assertEqual(list(d2txt[0].values()), [None, None, None])
        self.assertEqual(list(d2txt[1].values()), ["a", "b", "c"])
        self.assertEqual(list(d2txt[2].values()), ["foo", "bar", None])
        self.assertEqual(list(d2txt[3].values()), ["1", "2", "3"])
コード例 #2
0
ファイル: test_toml.py プロジェクト: pastelmind/d2txt
    def test_table_group_pack(self):
        """Tests if columns belonging to a table group are properly packed."""
        COLUMN_GROUPS.extend(
            initialize_column_groups((
                "--TableGroup",
                {
                    "col2": "column 2",
                    "col 1": "column 1",
                    "col4": "COLUMN 4"
                },
            )))
        d2txt = D2TXT(["column 1", "column 2", "column 3", "column 4"])
        d2txt.extend([["foo", "bar", 225, 45]])

        self.assertEqual(
            d2txt_to_toml(d2txt),
            "columns = [\n"
            "  'column 1',\n  'column 2',\n  'column 3',\n  'column 4',\n"
            "]\n\n"
            "[column_groups]\n"
            "--TableGroup = { col2 = 'column 2', 'col 1' = 'column 1', col4 = 'column 4' }\n\n"
            "[[rows]]\n"
            "--TableGroup = { col2 = 'bar', 'col 1' = 'foo', col4 = 45 }\n"
            "'column 3' = 225\n\n",
        )
コード例 #3
0
    def test_slice_syntax(self):
        """Tests if D2TXT accepts slice syntax assignment using lists."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        d2txt[:] = [
            [],
            ["foo", "bar"],
            ["1", "2", "3", "these", "strings", "are", "ignored"],
        ]
        self.assertEqual(len(d2txt), 3)
        for i, row in enumerate(d2txt):
            with self.subTest(i=i):
                self.assertEqual(len(row), 3)

        self.assertEqual(list(d2txt[0].values()), [None, None, None])
        self.assertEqual(list(d2txt[1].values()), ["foo", "bar", None])
        self.assertEqual(list(d2txt[2].values()), ["1", "2", "3"])

        d2txt[0:2] = [
            ["car", "bus", "cow"],
            ["one", "two", "three", "four"],
            ["portal"],
        ]
        self.assertEqual(len(d2txt), 4)
        for i, row in enumerate(d2txt):
            with self.subTest(i=i):
                self.assertEqual(len(row), 3)

        self.assertEqual(list(d2txt[0].values()), ["car", "bus", "cow"])
        self.assertEqual(list(d2txt[1].values()), ["one", "two", "three"])
        self.assertEqual(list(d2txt[2].values()), ["portal", None, None])
        self.assertEqual(list(d2txt[3].values()), ["1", "2", "3"])
コード例 #4
0
 def test_empty_txt(self):
     """Tests if a new D2TXT object has zero rows."""
     d2txt = D2TXT([])
     self.assertEqual(len(d2txt), 0)
     with self.assertRaises(IndexError):
         d2txt[0]  # pylint:disable=pointless-statement
     with self.assertRaises(IndexError):
         d2txt[0] = []
コード例 #5
0
ファイル: test_toml.py プロジェクト: pastelmind/d2txt
    def test_none_or_empty_string_ignored(self):
        """Tests if None or '' is ignored, but other falsy values are not."""
        d2txt = D2TXT(["int 0", "float 0.0", "False", "None", "empty"])
        d2txt.extend([[0, 0.0, False, None, ""]])

        self.assertEqual(
            d2txt_to_toml(d2txt),
            "columns = [\n  'int 0',\n  'float 0.0',\n  'False',\n  'None',\n  'empty',\n]\n\n"
            "[[rows]]\n'int 0' = 0\n'float 0.0' = 0.0\nFalse = false\n\n",
        )
コード例 #6
0
    def test_convert_row_to_list(self):
        """Tests if a D2TXT row can be converted to a list."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        d2txt.append(["foo", "bar", "baz"])
        self.assertEqual(list(d2txt[0].values()), ["foo", "bar", "baz"])

        d2txt[0]["column 1"] = "alpha"
        d2txt[0]["column 2"] = "beta"
        d2txt[0]["column 3"] = "gamma"
        self.assertEqual(list(d2txt[0].values()), ["alpha", "beta", "gamma"])
コード例 #7
0
    def test_cell_whitespace(self):
        """Tests if whitespace in cells are preserved when saved to a TXT file."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])
        d2txt.extend([["  2 leading spaces", "3 trailing spaces   ", "     "]])

        # newline='' is required to make csv.writer work correctly
        txtfile = StringIO(newline="")
        d2txt.to_txt(txtfile)
        self.assertEqual(
            txtfile.getvalue(),
            "column 1\tcolumn 2\tcolumn 3\r\n"
            "  2 leading spaces\t3 trailing spaces   \t     \r\n",
        )
コード例 #8
0
    def test_column_name_case_preserving(self):
        """Tests if column names are case-preserved when saved to a TXT file."""
        d2txt = D2TXT(["column name", "Column Name", "COLUMN NAME"])
        d2txt.extend([["lowercase", "Capitalized", "UPPERCASE"]])

        # newline='' is required to make csv.writer work correctly
        txtfile = StringIO(newline="")
        d2txt.to_txt(txtfile)
        self.assertEqual(
            txtfile.getvalue(),
            "column name\tColumn Name\tCOLUMN NAME\r\n"
            "lowercase\tCapitalized\tUPPERCASE\r\n",
        )
コード例 #9
0
    def test_surrounding_quotes(self):
        """Tests if surrounding quotes are preserved when saved to a TXT file."""
        d2txt = D2TXT(["'single quotes'", '"double quotes"', "`backticks`"])
        d2txt.extend([["'single quotes'", '"double quotes"', "`backticks`"]])

        # newline='' is required to make csv.writer work correctly
        txtfile = StringIO(newline="")
        d2txt.to_txt(txtfile)
        self.assertEqual(
            txtfile.getvalue(),
            "'single quotes'\t\"double quotes\"\t`backticks`\r\n"
            "'single quotes'\t\"double quotes\"\t`backticks`\r\n",
        )
コード例 #10
0
ファイル: test_toml.py プロジェクト: pastelmind/d2txt
    def test_nested_group_pack(self):
        """Tests if columns in multilevel groups are properly packed."""
        COLUMN_GROUPS.extend(
            initialize_column_groups(
                [
                    "--ArrayOfTables",
                    [
                        {
                            "min": "RedMin",
                            "max": "RedMax"
                        },
                        {
                            "min": "BlueMin",
                            "max": "BlueMax"
                        },
                    ],
                ],
                [
                    "--TableOfArrays", {
                        "weight": ["Weight1", "Weight2", "Weight3"]
                    }
                ],
            ))
        d2txt = D2TXT([
            "RedMin",
            "BlueMin",
            "RedMax",
            "BlueMax",
            "Weight2",
            "Weight3",
            "Weight1",
            "Misc",
        ])
        d2txt.extend([[10, 20, "unknown", 100, 0, 500, 1000, 4]])

        self.assertEqual(
            d2txt_to_toml(d2txt),
            "columns = [\n"
            "  'RedMin',\n  'BlueMin',\n  'RedMax',\n  'BlueMax',\n"
            "  'Weight2',\n  'Weight3',\n  'Weight1',\n  'Misc',\n"
            "]\n\n"
            "[column_groups]\n"
            "--ArrayOfTables = ["
            "{ min = 'RedMin', max = 'RedMax' }, { min = 'BlueMin', max = 'BlueMax' }"
            "]\n"
            "--TableOfArrays = { weight = ['Weight1', 'Weight2', 'Weight3'] }\n\n"
            "[[rows]]\n"
            "--ArrayOfTables = [{ min = 10, max = 'unknown' }, { min = 20, max = 100 }]\n"
            "--TableOfArrays = { weight = [1000, 0, 500] }\n"
            "Misc = 4\n\n",
        )
コード例 #11
0
    def test_cell_access(self):
        """Tests if cells can be accessed using row indices and column names."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        d2txt.append(["foo", "bar", "baz"])
        self.assertEqual(d2txt[0]["column 1"], "foo")
        self.assertEqual(d2txt[0]["column 2"], "bar")
        self.assertEqual(d2txt[0]["column 3"], "baz")

        d2txt[0]["column 1"] = "alpha"
        d2txt[0]["column 2"] = "beta"
        d2txt[0]["column 3"] = "gamma"
        self.assertEqual(d2txt[0]["column 1"], "alpha")
        self.assertEqual(d2txt[0]["column 2"], "beta")
        self.assertEqual(d2txt[0]["column 3"], "gamma")
コード例 #12
0
    def test_column_name_whitespace(self):
        """Tests if whitespace in columns are preserved when saved to a file."""
        d2txt = D2TXT(
            ["   column 1", "column 2    ", "  column 3  ", "", "  "])
        d2txt.extend(
            [["3 before", "4 after", "2 both", "empty", "spaces only"]])

        # newline='' is required to make csv.writer work correctly
        txtfile = StringIO(newline="")
        d2txt.to_txt(txtfile)
        self.assertEqual(
            txtfile.getvalue(),
            "   column 1\tcolumn 2    \t  column 3  \t\t  \r\n"
            "3 before\t4 after\t2 both\tempty\tspaces only\r\n",
        )
コード例 #13
0
    def test_save_to_file_object(self):
        """Tests if D2TXT can be saved to a file object."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])
        d2txt.extend([["value 1", "value 2", "value 3"], ["foo", "bar",
                                                          "baz"]])

        # newline='' is required to make csv.writer work correctly
        txtfile = StringIO(newline="")
        d2txt.to_txt(txtfile)
        self.assertEqual(
            txtfile.getvalue(),
            "column 1\tcolumn 2\tcolumn 3\r\n"
            "value 1\tvalue 2\tvalue 3\r\n"
            "foo\tbar\tbaz\r\n",
        )
コード例 #14
0
    def test_assign_list(self):
        """Tests if D2TXT accepts direct assignment using lists."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        d2txt.extend([[]] * 3)
        d2txt[0] = []
        d2txt[1] = ["foo", "bar"]
        d2txt[2] = ["1", "2", "3", "these", "strings", "are", "ignored"]

        self.assertEqual(len(d2txt), 3)
        for i, row in enumerate(d2txt):
            with self.subTest(i=i):
                self.assertEqual(len(row), 3)

        self.assertEqual(list(d2txt[0].values()), [None, None, None])
        self.assertEqual(list(d2txt[1].values()), ["foo", "bar", None])
        self.assertEqual(list(d2txt[2].values()), ["1", "2", "3"])
コード例 #15
0
    def test_save_to_path(self):
        """Tests if D2TXT can be saved to a file path."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])
        d2txt.extend([["value 1", "value 2", "value 3"], ["foo", "bar",
                                                          "baz"]])

        d2txt.to_txt(type(self).save_txt_path)
        # newline='' is required to make csv.writer work correctly
        with open(type(self).save_txt_path, newline="") as save_txt:
            saved_contents = save_txt.read()

        self.assertEqual(
            saved_contents,
            "column 1\tcolumn 2\tcolumn 3\r\n"
            "value 1\tvalue 2\tvalue 3\r\n"
            "foo\tbar\tbaz\r\n",
        )
コード例 #16
0
    def test_insert_list(self):
        """Tests if D2TXT.insert() accepts lists."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        d2txt.insert(0, [])
        self.assertEqual(len(d2txt), 1)
        d2txt.insert(0, ["foo", "bar"])
        self.assertEqual(len(d2txt), 2)
        d2txt.insert(1, ["1", "2", "3", "these", "strings", "are", "ignored"])
        self.assertEqual(len(d2txt), 3)

        for i, row in enumerate(d2txt):
            with self.subTest(i=i):
                self.assertEqual(len(row), 3)

        self.assertEqual(list(d2txt[0].values()), ["foo", "bar", None])
        self.assertEqual(list(d2txt[1].values()), ["1", "2", "3"])
        self.assertEqual(list(d2txt[2].values()), [None, None, None])
コード例 #17
0
    def test_column_name_case_sensitivity(self):
        """Tests if column names are case-sensitive."""
        d2txt = D2TXT(["column name", "Column Name", "COLUMN NAME"])

        d2txt.append(["lowercase", "capital letters", "uppercase"])
        self.assertEqual(
            list(d2txt[0].values()),
            ["lowercase", "capital letters", "uppercase"],
        )
        with self.assertRaises(KeyError):
            d2txt[0]["column NAME"]  # pylint:disable=pointless-statement

        d2txt[0]["COLUMN NAME"] = "c"
        d2txt[0]["Column Name"] = "b"
        d2txt[0]["column name"] = "a"
        self.assertEqual(list(d2txt[0].values()), ["a", "b", "c"])
        with self.assertRaises(KeyError):
            d2txt[0]["column NAME"] = 1
コード例 #18
0
ファイル: test_toml.py プロジェクト: pastelmind/d2txt
    def test_bitfield_decode(self):
        """Tests if bitfields are correctly decoded when saved to TOML file."""
        d2txt = D2TXT(["aurafilter"])
        d2txt.extend([["33025"], ["0"], ["65535"], ["4294901760"]])

        self.maxDiff = None  # pylint: disable=invalid-name
        self.assertEqual(
            d2txt_to_toml(d2txt),
            "columns = [\n  'aurafilter',\n]\n\n"
            "[[rows]]\naurafilter = [['FindPlayers', 'NotInsideTowns', 'IgnoreAllies']]\n\n"
            "[[rows]]\naurafilter = [[]]\n\n"
            "[[rows]]\naurafilter = "
            "[['FindPlayers', 'FindMonsters', 'FindOnlyUndead', 'FindMissiles', "
            "'FindObjects', 'FindItems', 'FindAttackable', 'NotInsideTowns', "
            "'UseLineOfSight', 'FindSelectable', 'FindCorpses', 'NotInsideTowns2', "
            "'IgnoreBoss', 'IgnoreAllies'], [0x840]]\n\n"
            "[[rows]]\naurafilter = "
            "[['IgnoreNPC', 'IgnorePrimeEvil', 'IgnoreJustHitUnits'], [0xFFF20000]]\n\n",
        )
コード例 #19
0
    def test_invalid_row_and_cell(self):
        """Tests if accessing invalid rows and cells raises appropriate exceptions."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])
        d2txt.append(["foo", "bar", "baz"])
        d2txt.append(["rabbit", "dog", "cow"])
        d2txt.append(["one", "two", "six"])

        with self.assertRaises(IndexError):
            d2txt[99]  # pylint:disable=pointless-statement
        with self.assertRaises(IndexError):
            d2txt[99] = ["mangy", "dog", "cow"]
        with self.assertRaises(IndexError):
            d2txt[99]["column 1"]  # pylint:disable=pointless-statement
        with self.assertRaises(IndexError):
            d2txt[99]["column 1"] = "cat"
        with self.assertRaises(KeyError):
            d2txt[0]["column 99"]  # pylint:disable=pointless-statement
        with self.assertRaises(KeyError):
            d2txt[0]["column 99"] = "bird"
コード例 #20
0
ファイル: test_toml.py プロジェクト: pastelmind/d2txt
    def test_array_group_pack(self):
        """Tests if columns belonging to an array group are properly packed."""
        COLUMN_GROUPS.extend(
            initialize_column_groups(
                ("--ArrayGroup", ("column 2", "column 1", "COLUMN 4"))))
        d2txt = D2TXT(["column 1", "column 2", "column 3", "column 4"])
        d2txt.extend([["foo", "bar", 225, 45]])

        self.assertEqual(
            d2txt_to_toml(d2txt),
            "columns = [\n"
            "  'column 1',\n  'column 2',\n  'column 3',\n  'column 4',\n"
            "]\n\n"
            "[column_groups]\n"
            "--ArrayGroup = ['column 2', 'column 1', 'column 4']\n\n"
            "[[rows]]\n"
            "--ArrayGroup = ['bar', 'foo', '45']\n"
            "'column 3' = 225\n\n",
        )
コード例 #21
0
    def test_extend_list(self):
        """Tests if D2TXT.extend() accepts lists."""
        d2txt = D2TXT(["column 1", "column 2", "column 3"])

        # Internally uses D2TXT.append(), which uses D2TXT.insert()
        d2txt.extend([[]])
        self.assertEqual(len(d2txt), 1)
        d2txt.extend([
            ["foo", "bar"],
            ["1", "2", "3", "these", "strings", "are", "ignored"],
        ])
        self.assertEqual(len(d2txt), 3)

        for i, row in enumerate(d2txt):
            with self.subTest(i=i):
                self.assertEqual(len(row), 3)

        self.assertEqual(list(d2txt[0].values()), [None, None, None])
        self.assertEqual(list(d2txt[1].values()), ["foo", "bar", None])
        self.assertEqual(list(d2txt[2].values()), ["1", "2", "3"])
コード例 #22
0
    def test_falsy_cell(self):
        """Tests if falsy values in cells are preserved when saved to a file.

        Tested values include None, integer 0, float 0.0, and False.
        """
        d2txt = D2TXT(["column 1", "column 2", "column 3"])
        d2txt.extend([
            ["empty", "", ""],
            ["", "", ""],
            ["None", None, None],
            [None, None, None],
            ["integer 0", 0, 0],
            [0, 0, 0],
            ["float 0.0", 0.0, 0.0],
            [0.0, 0.0, 0.0],
            ["False", False, False],
            [False, False, False],
            ["truncated row"],
            [],
        ])

        # newline='' is required to make csv.writer work correctly
        txtfile = StringIO(newline="")
        d2txt.to_txt(txtfile)
        self.assertEqual(
            txtfile.getvalue(),
            "column 1\tcolumn 2\tcolumn 3\r\n"
            "empty\t\t\r\n"
            "\t\t\r\n"
            "None\t\t\r\n"
            "\t\t\r\n"
            "integer 0\t0\t0\r\n"
            "0\t0\t0\r\n"
            "float 0.0\t0.0\t0.0\r\n"
            "0.0\t0.0\t0.0\r\n"
            "False\tFalse\tFalse\r\n"
            "False\tFalse\tFalse\r\n"
            "truncated row\t\t\r\n"
            "\t\t\r\n",
        )
コード例 #23
0
 def test_duplicate_column_names(self):
     """Tests if duplicate column names cause an error."""
     with self.assertRaises(DuplicateColumnNameError):
         D2TXT(["column name"] * 60)
コード例 #24
0
 def test_column_assignment(self):
     """Tests column name assignment."""
     base_column_names = ["column 1", "column 2", "column 3"]
     d2txt = D2TXT(base_column_names)
     self.assertEqual(list(d2txt.column_names()), base_column_names)