File: parse_json_option_spec.rb

package info (click to toggle)
ruby-html-proofer 3.19.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ruby: 3,203; sh: 9; makefile: 4; javascript: 1; php: 1
file content (47 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'spec_helper'
require 'html-proofer'

describe 'JSON config parser' do
  it 'Throws an error when the option name is not a string' do
    expect { HTMLProofer::Configuration.parse_json_option(123, '') }.to raise_error(ArgumentError, 'Must provide an option name in string format.')
  end

  it 'Throws an error when the option name is empty' do
    expect { HTMLProofer::Configuration.parse_json_option('', '{}') }.to raise_error(ArgumentError, 'Must provide an option name in string format.')
  end

  it 'Throws an error when the option name is whitespace' do
    expect { HTMLProofer::Configuration.parse_json_option('    ', '{}') }.to raise_error(ArgumentError, 'Must provide an option name in string format.')
  end

  it 'Throws an error when the json config is not a string' do
    expect { HTMLProofer::Configuration.parse_json_option('testName', 123) }.to raise_error(ArgumentError, 'Must provide a JSON configuration in string format.')
  end

  it 'returns an empty options object when config is nil' do
    result = HTMLProofer::Configuration.parse_json_option('testName', nil)
    expect(result).to eq({})
  end

  it 'returns an empty options object when config is empty' do
    result = HTMLProofer::Configuration.parse_json_option('testName', '')
    expect(result).to eq({})
  end

  it 'returns an empty options object when config is whitespace' do
    result = HTMLProofer::Configuration.parse_json_option('testName', '    ')
    expect(result).to eq({})
  end

  it 'Returns an object representing the json when valid json' do
    result = HTMLProofer::Configuration.parse_json_option('testName', '{ "myValue": "hello world!", "numberValue": 123}')
    expect(result[:myValue]).to eq('hello world!')
    expect(result[:numberValue]).to eq(123)
  end

  it 'Throws an error when the json config is not valid json' do
    expect { HTMLProofer::Configuration.parse_json_option('testName', 'abc') }.to raise_error(ArgumentError)
  end
end