def test_check_if_parameter_is_defined(self):
        v = ApiaryValidator()
        v.state = _state_read_param_tag
        v._parameters = ['p1', 'p2', 'p3']

        valid, error = v._read_line('+ p1   (string) ... test parameter 1')
        self.assertTrue(valid)

        valid, error = v._read_line('       + p2 (string) ... test parameter 2')
        self.assertTrue(valid)

        valid, error = v._read_line('+ p4 (string) ... test parameter 3')
        self.assertFalse(valid)
        self.assertEqual(error.type, ApiaryParameterNotDefinedError(parameter='p4').type)
    def test_check_if_parameter_is_defined(self):
        v = ApiaryValidator()
        v.state = _state_read_param_tag
        v._parameters = ['p1', 'p2', 'p3']

        valid, error = v._read_line('+ p1   (string) ... test parameter 1')
        self.assertTrue(valid)

        valid, error = v._read_line(
            '       + p2 (string) ... test parameter 2')
        self.assertTrue(valid)

        valid, error = v._read_line('+ p4 (string) ... test parameter 3')
        self.assertFalse(valid)
        self.assertEqual(error.type,
                         ApiaryParameterNotDefinedError(parameter='p4').type)
    def test_read_api_title_with_parameters(self):
        v = ApiaryValidator()
        test_title = '## TEST API [/test/api/pattern/{param_1}/with/{param_2}/{?p1,p2,p3}]'
        expected = ['param_1', 'param_2', 'p1', 'p2', 'p3']

        for state in [_state_read_group_title, _state_read_response_tag]:
            v._parameters.clear()
            v.state = state
            # load the response content if necessary:
            if state == _state_read_response_tag:
                content_file = path.join(path.dirname(path.abspath(__file__)),
                                         'response',
                                         'response_good_example001.json')
                ApiaryTest._load_content_to_validator(v, content_file)

            v._read_line(test_title)
            results = v._parameters
            self.assertEqual(results, expected)
    def test_read_api_title_with_parameters(self):
        v = ApiaryValidator()
        test_title = '## TEST API [/test/api/pattern/{param_1}/with/{param_2}/{?p1,p2,p3}]'
        expected = ['param_1', 'param_2', 'p1', 'p2', 'p3']

        for state in [_state_read_group_title, _state_read_response_tag]:
            v._parameters.clear()
            v.state = state
            # load the response content if necessary:
            if state == _state_read_response_tag:
                content_file = path.join(path.dirname(path.abspath(__file__)),
                                         'response',
                                         'response_good_example001.json')
                ApiaryTest._load_content_to_validator(v, content_file)

            v._read_line(test_title)
            results = v._parameters
            self.assertEqual(results, expected)
    def test_read_line_with_indent_validation(self):
        v = ApiaryValidator()
        valid_examples = [' ', '        {', '\t\t{']
        invalid_examples = ['"test":123, ', '       "test": false, ']

        for state in [_state_read_response_tag, _state_read_request_tag]:

            for line in valid_examples:
                v.state = state
                v.decoder.clear()
                valid, error = v._read_line(line)
                if error and DEBUG:
                    print('\nUnexpected Error: %s' % error)
                self.assertTrue(valid)
                self.assertEqual(error, None)

            for line in invalid_examples:
                v.state = state
                v.decoder.clear()
                valid, error = v._read_line(line)
                self.assertFalse(valid)
                self.assertEqual(error.type, ApiarySyntaxError().type)
    def test_read_line_with_indent_validation(self):
        v = ApiaryValidator()
        valid_examples   = [' ', '        {', '\t\t{']
        invalid_examples = ['"test":123, ', '       "test": false, ']

        for state in [_state_read_response_tag, _state_read_request_tag]:

            for line in valid_examples:
                v.state = state
                v.decoder.clear()
                valid, error = v._read_line(line)
                if error and DEBUG:
                    print('\nUnexpected Error: %s' % error)
                self.assertTrue(valid)
                self.assertEqual(error, None)

            for line in invalid_examples:
                v.state = state
                v.decoder.clear()
                valid, error = v._read_line(line)
                self.assertFalse(valid)
                self.assertEqual(error.type, ApiarySyntaxError().type)
 def test_read_line_state_read_response_tag_with_consecutive_responses(self):
     v = ApiaryValidator()
     response_file = path.join(_current_file_path, 'response', 'response_good_example001.json')
     # setup the validator
     v.state = _state_read_response_tag
     v._read_newline = True
     self._load_content_to_validator(v, content_file=response_file)
     # test with the 2nd response tag
     valid, error = v._read_line(_TEST_RESPONSE_TAG)
     if error and DEBUG:
         print('\nUnexpected Error: %s' % error)
     self.assertTrue(valid)
     self.assertFalse(v._read_newline)
     self.assertIsNone(error)
    def test_for_check_code_block_newline(self):
        v = ApiaryValidator()
        # valid cases:
        newline_examples = [' ', '\n', '\r', '   \n', '     \r']
        for newline in newline_examples:
            for state in [_state_read_response_tag, _state_read_request_tag]:
                v.decoder.clear()
                v.state = state
                v._read_newline = False
                v._read_line(newline)
                valid, error = v._read_line('        {')
                if error and DEBUG:
                    print('\nUnexpected Error: %s' % error)
                self.assertTrue(valid)
                self.assertIsNone(error)

        # invalid cases:
        for state in [_state_read_request_tag, _state_read_request_tag]:
            v.decoder.clear()
            v.state = state
            v._read_newline = False
            valid, error = v._read_line('        {')
            self.assertFalse(valid)
            self.assertEqual(error.type, ApiarySyntaxError().type)
    def test_for_check_code_block_newline(self):
        v = ApiaryValidator()
        # valid cases:
        newline_examples = [' ', '\n', '\r', '   \n', '     \r']
        for newline in newline_examples:
            for state in [_state_read_response_tag, _state_read_request_tag]:
                v.decoder.clear()
                v.state = state
                v._read_newline = False
                v._read_line(newline)
                valid, error = v._read_line('        {')
                if error and DEBUG:
                    print('\nUnexpected Error: %s' % error)
                self.assertTrue(valid)
                self.assertIsNone(error)

        # invalid cases:
        for state in [_state_read_request_tag, _state_read_request_tag]:
            v.decoder.clear()
            v.state = state
            v._read_newline = False
            valid, error = v._read_line('        {')
            self.assertFalse(valid)
            self.assertEqual(error.type, ApiarySyntaxError().type)
 def test_read_line_state_read_response_tag_with_consecutive_responses(
         self):
     v = ApiaryValidator()
     response_file = path.join(_current_file_path, 'response',
                               'response_good_example001.json')
     # setup the validator
     v.state = _state_read_response_tag
     v._read_newline = True
     self._load_content_to_validator(v, content_file=response_file)
     # test with the 2nd response tag
     valid, error = v._read_line(_TEST_RESPONSE_TAG)
     if error and DEBUG:
         print('\nUnexpected Error: %s' % error)
     self.assertTrue(valid)
     self.assertFalse(v._read_newline)
     self.assertIsNone(error)