|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class FilterTest < ActiveSupport::TestCase |
| 4 | + test "returns the original value without options" do |
| 5 | + parser = Brita::ValueParser.new(value: "hi") |
| 6 | + |
| 7 | + assert_equal "hi", parser.parse |
| 8 | + end |
| 9 | + |
| 10 | + test "With options an array of integers results in an array of integers" do |
| 11 | + options = { |
| 12 | + supports_ranges: true, |
| 13 | + supports_json: true |
| 14 | + } |
| 15 | + parser = Brita::ValueParser.new(value: [1,2,3]) |
| 16 | + |
| 17 | + assert_equal [1,2,3], parser.parse |
| 18 | + end |
| 19 | + |
| 20 | + test "With options a json string array of integers results in an array of integers" do |
| 21 | + options = { |
| 22 | + supports_ranges: true, |
| 23 | + supports_json: true |
| 24 | + } |
| 25 | + parser = Brita::ValueParser.new(value: "[1,2,3]", options: options) |
| 26 | + |
| 27 | + assert_equal [1,2,3], parser.parse |
| 28 | + end |
| 29 | + |
| 30 | + test "with invalid json returns original value" do |
| 31 | + options = { |
| 32 | + supports_ranges: true, |
| 33 | + supports_json: true |
| 34 | + } |
| 35 | + parser = Brita::ValueParser.new(value: "[1,2,3", options: options) |
| 36 | + |
| 37 | + assert_equal "[1,2,3", parser.parse |
| 38 | + end |
| 39 | + |
| 40 | + test "JSON parsing only supports arrays" do |
| 41 | + options = { |
| 42 | + supports_json: true |
| 43 | + } |
| 44 | + json_string = "{\"a\":4}" |
| 45 | + parser = Brita::ValueParser.new(value: json_string, options: options) |
| 46 | + |
| 47 | + assert_equal json_string, parser.parse |
| 48 | + end |
| 49 | + |
| 50 | + test "With options a range string of integers results in a range" do |
| 51 | + options = { |
| 52 | + supports_ranges: true, |
| 53 | + supports_json: true |
| 54 | + } |
| 55 | + parser = Brita::ValueParser.new(value: "1...3", options: options) |
| 56 | + |
| 57 | + assert_instance_of Range, parser.parse |
| 58 | + end |
| 59 | + |
| 60 | + test "parses true from 1" do |
| 61 | + options = { |
| 62 | + supports_boolean: true |
| 63 | + } |
| 64 | + parser = Brita::ValueParser.new(value: 1, options: options) |
| 65 | + |
| 66 | + assert_equal true, parser.parse |
| 67 | + end |
| 68 | + |
| 69 | + test "parses false from 0" do |
| 70 | + options = { |
| 71 | + supports_boolean: true |
| 72 | + } |
| 73 | + parser = Brita::ValueParser.new(value: 0, options: options) |
| 74 | + |
| 75 | + assert_equal false, parser.parse |
| 76 | + end |
| 77 | +end |
0 commit comments