Module: JsonAssert::Assertions

Includes:
Exclusion, Helpers
Defined in:
lib/json_assert/assertions.rb

Instance Method Summary collapse

Methods included from Exclusion

#exclude_key?, #exclude_keys, #excluded_keys

Methods included from Helpers

#generate_normalized_json, #normalize_json, #parse_json

Instance Method Details

#assert_include_json(actual_json, expected_json, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/json_assert/assertions.rb', line 24

def assert_include_json(actual_json, expected_json, options={})
  parse_options(options)
  actual = parse_json(actual_json, @path)
  expected = exclude_keys(parse_json(expected_json))
  msg = build_message(@message, "<?> expected to include\n<?>.", expected.to_s, actual.to_s)
  assert_block(msg) {
    case actual
    when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected)
    when Array then actual.map{|e| exclude_keys(e) }.include?(expected)
    else false
    end
  }
end

#assert_json_eql(actual_json, expected_json, options = {}) ⇒ Object



10
11
12
13
14
15
# File 'lib/json_assert/assertions.rb', line 10

def assert_json_eql(actual_json, expected_json, options={})
  parse_options(options)
  actual, expected = scrub(actual_json, @path), [scrub(expected_json)]
  msg = build_message(@message, "<?> expected to be equal to\n<?>.", expected.inspect, actual.inspect)
  assert_block(msg) { actual == expected.first }
end

#assert_json_not_eql(actual_json, expected_json, options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/json_assert/assertions.rb', line 17

def assert_json_not_eql(actual_json, expected_json, options={})
  parse_options(options)
  actual, expected = scrub(actual_json, @path), [scrub(expected_json)]
  msg = build_message(@message, "<?> expected not to be equal to\n<?>.", expected.to_s, actual.to_s)
  assert_block(msg) { actual != expected.first }
end

#assert_json_path(json, path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/json_assert/assertions.rb', line 38

def assert_json_path(json, path)
  msg = "path was not found for #{path}"
  assert_block(msg) do
    begin
      parse_json(json, path)
      true
    rescue JsonAssert::MissingPathError
      false
    end
  end
end

#assert_json_size(json, expected_size, options = {}) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/json_assert/assertions.rb', line 85

def assert_json_size(json, expected_size, options={})
  parse_options(options)
  @json = json
  size = actual_size
  msg = build_message(@message, "expected size was <?> but it was <?>.", expected_size.to_s, size.to_s)
  assert_block(msg) { size == expected_size }
end

#assert_json_type(json, klass, options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/json_assert/assertions.rb', line 78

def assert_json_type(json, klass, options={})
  parse_options(options)
  actual = parse_json(json, @path)
  msg = build_message(@message, "<?> expected to be a\n<?>.", actual.to_s, klass.to_s)
  assert_block(msg) { actual.is_a?(klass) }
end

#assert_not_json_path(json, path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/json_assert/assertions.rb', line 50

def assert_not_json_path(json, path)
  msg = "path was unexpectedly found for #{path}"
  assert_block(msg) do
    begin
      parse_json(json, path)
      false
    rescue JsonAssert::MissingPathError
      true
    end
  end
end

#assert_not_part_json(json, json_part, options = {}) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/json_assert/assertions.rb', line 70

def assert_not_part_json(json, json_part, options={})
  parse_options(options)
  @json = parse_json(json, @path)
  @json_part = parse_json(json_part)
  msg = build_message(@message, "<?> was unexpectedly part of \n<?>.", @json_part.to_s, @json.to_s)
  assert_block(msg){ !find_part_json }
end

#assert_part_json(json, json_part, options = {}) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/json_assert/assertions.rb', line 62

def assert_part_json(json, json_part, options={})
  parse_options(options)
  @json = parse_json(json, @path)
  @json_part = parse_json(json_part)
  msg = build_message(@message, "<?> was not part of \n<?>.", @json_part.to_s, @json.to_s)
  assert_block(msg){ find_part_json == true }
end