コード例 #1
0
def test_level_parser_properties():
    parser = LevelParser()
    assert parser.levels == 1
    assert parser.parsers == [Parser()]

    with pytest.raises(ValueError):
        parser.levels = 0
    with pytest.raises(ValueError):
        parser.levels = -1
    assert parser.levels == 1

    parser.levels = 2
    assert parser.parsers == [Parser(), Parser()]

    level = Parser(state_sizes=[2, 3])
    parser.parsers = level
    assert parser.parsers == [level, level]

    parser.parsers = [Parser(), level, Parser()]
    assert parser.parsers == [Parser(), level]

    parser.levels = 1
    assert parser.parsers == [Parser()]

    parser.levels = 2
    assert parser.parsers[1] is level
コード例 #2
0
    def test_properties(self):
        parser = LevelParser()
        self.assertEqual(parser.levels, 1)
        self.assertEqual(parser.parsers, [Parser()])

        with self.assertRaises(ValueError):
            parser.levels = 0
        with self.assertRaises(ValueError):
            parser.levels = -1
        self.assertEqual(parser.levels, 1)

        parser.levels = 2
        self.assertEqual(parser.parsers, [Parser(), Parser()])

        level = Parser(state_sizes=[2, 3])
        parser.parsers = level
        self.assertEqual(parser.parsers, [level, level])

        parser.parsers = [Parser(), level, Parser()]
        self.assertEqual(parser.parsers, [Parser(), level])

        parser.levels = 1
        self.assertEqual(parser.parsers, [Parser()])

        parser.levels = 2
        self.assertIs(parser.parsers[1], level)
コード例 #3
0
def test_level_parser_parse(mocker, test, res):
    level_dataset = mocker.patch('markovchain.parser.level_dataset',
                                 return_value='0')
    parsers = [
        Mock(wraps=ParserBase(lambda x: [0])),
        Mock(wraps=ParserBase(lambda x: [1]))
    ]
    parser = LevelParser(levels=2, parsers=parsers)
    assert list(parser(test, dataset='data')) == res
    if test:
        assert level_dataset.call_count == 2
        level_dataset.assert_has_calls([call(0), call(1)])
    for parser, data in zip(parsers, test):
        parser.assert_called_once_with(data, False, 'data0')
コード例 #4
0
 def test_parse(self):
     parser = LevelParser(
         levels=2,
         parsers=[self.ParserTest(lambda x: [0]),
                  self.ParserTest(lambda x: [1])]
     )
     self.assertEqual(list(parser([[0], [1]])), [0, 1])
     self.assertEqual(list(parser([[0]] * 5)), [0, 1])
     self.assertEqual(list(parser([])), [])
コード例 #5
0
def test_level_parser_save_load():
    level = Parser(state_sizes=[2, 3])
    parser = LevelParser(levels=3, parsers=[level, Parser()])
    saved = parser.save()
    loaded = Parser.load(saved)
    assert parser == loaded
コード例 #6
0
def test_level_parser_eq(test, test2, res):
    assert (LevelParser(*test) == LevelParser(*test2)) == res
コード例 #7
0
def test_level_parser_reset():
    parsers = [Mock(), Mock()]
    parser = LevelParser(levels=2, parsers=parsers)
    parser.reset()
    for level in parsers:
        level.reset.assert_called_once_with()
コード例 #8
0
 def test_save_load(self):
     level = Parser(state_sizes=[2, 3])
     parser = LevelParser(levels=3, parsers=[level, Parser()])
     saved = parser.save()
     loaded = Parser.load(saved)
     self.assertEqual(parser, loaded)
コード例 #9
0
 def test_reset(self):
     parser = LevelParser(levels=2,
                          parsers=[self.ParserTest(), self.ParserTest()])
     self.assertEqual([x.is_reset for x in parser.parsers], [False, False]) # pylint:disable=no-member
     parser.reset()
     self.assertEqual([x.is_reset for x in parser.parsers], [True, True]) # pylint:disable=no-member