Beispiel #1
0
 def setUp(self):
     self.p = UKHASParser(FakeParser())
Beispiel #2
0
class TestUKHASParser:
    """UKHAS Parser"""
    def setUp(self):
        self.p = UKHASParser(FakeParser())

    def test_pre_parse_rejects_bad_sentences(self):
        for sentence in bad_sentences:
            assert_raises(ValueError, self.p.pre_parse, sentence)

    def test_pre_parse_accepts_good_setences(self):
        for sentence in good_sentences:
            assert self.p.pre_parse(sentence) == "good"

    def test_pre_parse_rejects_bad_callsigns(self):
        for callsign in bad_callsigns:
            sentence = callsign_template.format(callsign)
            assert_raises(ValueError, self.p.pre_parse, sentence)

    def test_pre_parse_accepts_good_callsigns(self):
        for callsign in good_callsigns:
            sentence = callsign_template.format(callsign)
            assert self.p.pre_parse(sentence) == callsign

    def test_pre_parse_rejects_bad_checksums(self):
        for checksum in bad_checksums:
            sentence = checksum_template.format(checksum)
            assert_raises(ValueError, self.p.pre_parse, sentence)

    def test_pre_parse_accepts_good_checksums(self):
        for checksum in good_checksums:
            sentence = checksum_template.format(checksum)
            assert self.p.pre_parse(sentence) == "good"

    def test_parse_rejects_bad_sentences(self):
        for sentence in bad_sentences:
            assert_raises(ValueError, self.p.parse, sentence, base_config)

    def test_parse_rejects_invalid_configs(self):
        for config in [
                config_no_protocol, config_no_checksum, config_no_fields,
                config_empty_fields, config_field_without_name,
                config_field_without_type, config_field_without_format,
                config_checksum_invalid, config_field_type_invalid,
                config_format_invalid, config_name_invalid,
                config_invalid_protocol
            ]:
            assert_raises(ValueError, self.p.parse, sentence_config, config)

    def test_parse_parses_correct_checksums(self):
        for sentence, config in [
                [sentence_no_checksum, config_checksum_none],
                [sentence_crc16_ccitt, config_checksum_crc16_ccitt],
                [sentence_xor, config_checksum_xor],
                [sentence_fletcher_16, config_checksum_fletcher_16],
                [sentence_fletcher_16_256, config_checksum_fletcher_16_256]
            ]:
            assert (self.p.parse(sentence, config) ==
                    self.output_append_sentence(output_checksum_test,
                        sentence))

    def test_parse_rejects_incorrect_checksums(self):
        for sentence, config in [
                [sentence_bad_crc16_ccitt, config_checksum_crc16_ccitt],
                [sentence_no_checksum, config_checksum_crc16_ccitt],
                [sentence_bad_xor, config_checksum_xor],
                [sentence_no_checksum, config_checksum_xor],
                [sentence_bad_fletcher_16, config_checksum_fletcher_16],
                [sentence_no_checksum, config_checksum_fletcher_16],
                [sentence_no_checksum, config_checksum_fletcher_16_256],
                [sentence_bad_fletcher_16_256, config_checksum_fletcher_16_256]
            ]:
            assert_raises(ValueError, self.p.parse, sentence, config)

    def test_parse_rejects_invalid_values(self):
        for sentence in [
                sentence_bad_int, sentence_bad_float, sentence_bad_time]:
            assert_raises(ValueError, self.p.parse, sentence,
                    config_checksum_none)

    def test_parse_rejects_bad_minutes(self):
        assert_raises(ValueError, self.p.parse, sentence_bad_minutes,
                config_minutes)

    def test_parse_parses_good_sentences(self):
        for sentence, output, config in [
                [sentence_good, output_good, base_config],
                [sentence_good_2, output_good_2, base_config],
                [sentence_good_3, output_good, config_minutes],
                [sentence_good_4, output_good, base_config],
                [sentence_good_5, output_good, base_config],
                [sentence_good_6, output_good_6, base_config]
            ]:
            assert (self.p.parse(sentence, config) ==
                    self.output_append_sentence(output, sentence))

    def test_parse_handles_shorter_sentences(self):
        assert (self.p.parse(sentence_short, base_config) ==
                self.output_append_sentence(output_short, sentence_short))

    def test_parse_handles_longer_sentences(self):
        assert (self.p.parse(sentence_long, base_config)
                == self.output_append_sentence(output_long, sentence_long))

    def output_append_sentence(self, output, sentence):
        output_copy = deepcopy(output)
        output_copy["_sentence"] = sentence
        return output_copy