<commit_before>require 'spec_helper'

describe APIMatchers::ResponseBody::HaveJsonNode do
  describe "actual.should have_json_node" do
    context 'expected key and value in top level' do
      it "should pass when the expected key exist" do
        { :product => 'gateway' }.to_json.should have_json_node(:product)
      end

      it "should fail when the expected key does not exist" do
        expect {
          { :product => 'pabx' }.to_json.should have_json_node(:developers)
        }.to fail_with(%Q{expected to have node called: 'developers'. Got: '{"product":"pabx"}'})
      end

      it "should pass when the expected key exist with the expected value" do
        { :product => 'payment-gateway' }.to_json.should have_json_node(:product).with('payment-gateway')
      end

      it "should pass when the expected key exist with the expected value (as integer)" do
        { :number => 1 }.to_json.should have_json_node(:number).with(1)
      end

      it "should pass when the expected key exist with the expected value (as boolean, true)" do
        { :number => true }.to_json.should have_json_node(:number).with(true)
      end

      it "should pass when the expected key exist with the expected value (as boolean, false)" do
        { :number => false }.to_json.should have_json_node(:number).with(false)
      end

      it "should pass when the expected key exist but the expected value is wrong (as boolean, true)" do
        { :number => true }.to_json.should_not have_json_node(:number).with(false)
      end

      it "should pass when the expected key exist but the expected value is wrong (as boolean, false)" do
        { :number => false }.to_json.should_not have_json_node(:number).with(true)
      end

      it "should pass when the expected key exists with the expected value (as DateTime)" do
        now = DateTime.now
        { :date => now }.to_json.should have_json_node(:date).with(now)
      end

      it "should pass when the expected key exists with the expected value (as Date)" do
        now = Date.today
        { :date => now }.to_json.should have_json_node(:date).with(now)
      end

      it "should pass when the expected key exists with the expected value (as Time)" do
        now = Time.now
        { :date => now }.to_json.should have_json_node(:date).with(now)
      end

      it "should fail when the expected key exist but the expected value don't exist" do
        expect {
          { :product => 'payment-gateway' }.to_json.should have_json_node(:product).with('email-marketing')
        }.to fail_with(%Q{expected to have node called: 'product' with value: 'email-marketing'. Got: '{"product":"payment-gateway"}'})
      end

      it "should not parse the matcher for json when you pass a xml" do
        expect {
          "<product><name>webdesk</name></product>".should have_json_node(:name).with('webdesk')
        }.to fail_with(%Q{expected to have node called: 'name' with value: 'webdesk'. Got: '<product><name>webdesk</name></product>'})
      end
    end

    context 'expected key and nil value' do
      it "should pass when the expected key exists" do
        { :product => nil }.to_json.should have_json_node(:product)
      end

      it "should pass when the expected key exists and the expected value is nil" do
        { :product => nil }.to_json.should have_json_node(:product).with( nil )
      end
<commit_msg>Time as key in the spec.<commit_after>require 'spec_helper'

describe APIMatchers::ResponseBody::HaveJsonNode do
  describe "actual.should have_json_node" do
    context 'expected key and value in top level' do
      it "should pass when the expected key exist" do
        { :product => 'gateway' }.to_json.should have_json_node(:product)
      end

      it "should fail when the expected key does not exist" do
        expect {
          { :product => 'pabx' }.to_json.should have_json_node(:developers)
        }.to fail_with(%Q{expected to have node called: 'developers'. Got: '{"product":"pabx"}'})
      end

      it "should pass when the expected key exist with the expected value" do
        { :product => 'payment-gateway' }.to_json.should have_json_node(:product).with('payment-gateway')
      end

      it "should pass when the expected key exist with the expected value (as integer)" do
        { :number => 1 }.to_json.should have_json_node(:number).with(1)
      end

      it "should pass when the expected key exist with the expected value (as boolean, true)" do
        { :number => true }.to_json.should have_json_node(:number).with(true)
      end

      it "should pass when the expected key exist with the expected value (as boolean, false)" do
        { :number => false }.to_json.should have_json_node(:number).with(false)
      end

      it "should pass when the expected key exist but the expected value is wrong (as boolean, true)" do
        { :number => true }.to_json.should_not have_json_node(:number).with(false)
      end

      it "should pass when the expected key exist but the expected value is wrong (as boolean, false)" do
        { :number => false }.to_json.should_not have_json_node(:number).with(true)
      end

      it "should pass when the expected key exists with the expected value (as DateTime)" do
        now = DateTime.now
        { :date => now }.to_json.should have_json_node(:date).with(now)
      end

      it "should pass when the expected key exists with the expected value (as Date)" do
        now = Date.today
        { :date => now }.to_json.should have_json_node(:date).with(now)
      end

      it "should pass when the expected key exists with the expected value (as Time)" do
        now = Time.now
        { :time => now }.to_json.should have_json_node(:time).with(now)
      end

      it "should fail when the expected key exist but the expected value don't exist" do
        expect {
          { :product => 'payment-gateway' }.to_json.should have_json_node(:product).with('email-marketing')
        }.to fail_with(%Q{expected to have node called: 'product' with value: 'email-marketing'. Got: '{"product":"payment-gateway"}'})
      end

      it "should not parse the matcher for json when you pass a xml" do
        expect {
          "<product><name>webdesk</name></product>".should have_json_node(:name).with('webdesk')
        }.to fail_with(%Q{expected to have node called: 'name' with value: 'webdesk'. Got: '<product><name>webdesk</name></product>'})
      end
    end

    context 'expected key and nil value' do
      it "should pass when the expected key exists" do
        { :product => nil }.to_json.should have_json_node(:product)
      end

      it "should pass when the expected key exists and the expected value is nil" do
        { :product => nil }.to_json.should have_json_node(:product).with( nil )
      end

{'fraction_non_alphanumeric': 0.10598265471115684, 'fraction_numerical': 0.0005879758929883875, 'mean_word_length': 2.9443478260869567, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437244', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1935, 'n_words': 717}