File: number_validation.rb

package info (click to toggle)
ruby-json-schema 2.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 5,806; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 1,819 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module NumberValidation
  module MinMaxTests
    def test_minimum
      schema = {
        'properties' => {
          'a' => { 'minimum' => 5 }
        }
      }

      assert_valid schema, {'a' => 5}
      assert_valid schema, {'a' => 6}

      refute_valid schema, {'a' => 4}
      refute_valid schema, {'a' => 4.99999}

      # other types are disregarded
      assert_valid schema, {'a' => 'str'}
    end

    def test_exclusive_minimum
      schema = {
        'properties' => {
          'a' => { 'minimum' => 5 }.merge(exclusive_minimum)
        }
      }

      assert_valid schema, {'a' => 6}
      assert_valid schema, {'a' => 5.0001}
      refute_valid schema, {'a' => 5}
    end

    def test_maximum
      schema = {
        'properties' => {
          'a' => { 'maximum' => 5 }
        }
      }

      assert_valid schema, {'a' => 4}
      assert_valid schema, {'a' => 5}

      refute_valid schema, {'a' => 6}
      refute_valid schema, {'a' => 5.0001}
    end

    def test_exclusive_maximum
      schema = {
        'properties' => {
          'a' => { 'maximum' => 5 }.merge(exclusive_maximum)
        }
      }

      assert_valid schema, {'a' => 4}
      assert_valid schema, {'a' => 4.99999}
      refute_valid schema, {'a' => 5}
    end
  end

  # draft3 introduced `divisibleBy`, renamed to `multipleOf` in draft4.
  # Favor the newer name, but the behavior should be identical.
  module MultipleOfTests
    def multiple_of
      'multipleOf'
    end

    def test_multiple_of
      schema = {
        'properties' => {
          'a' => { multiple_of => 1.1 }
        }
      }

      assert_valid schema, {'a' => 0}

      assert_valid schema, {'a' => 2.2}
      refute_valid schema, {'a' => 3.4}

      # other types are disregarded
      assert_valid schema, {'a' => 'hi'}
    end
  end
end