<commit_before>require 'spec_helper'

def stub_records(strings)
  records = strings.map { |s| Dnsruby::RR.new_from_string(s) }
  resolver = Dnsruby::Resolver.new
  allow(Dnsruby::Resolver).to receive(:new) do
    yield if block_given?
    resolver
  end
  allow(resolver).to receive_message_chain(:query, :answer).and_return(records)
end

describe 'rspec-dns matchers' do

  describe '#have_dns' do
    context 'with a sigle record' do
      it 'can evalutate an A record' do
        stub_records(['example.com 86400 A 192.168.100.100'])

        expect('example.com').to have_dns.with_type('A')
        expect('example.com').to_not have_dns.with_type('TXT')
        expect('example.com').to have_dns.with_type('A').and_address('192.168.100.100')
      end

      it 'can evalutate a AAAA record' do
        stub_records(['example.com 86400 AAAA 2001:0002:6c::430'])

        expect('example.com').to have_dns.with_type('AAAA')
        expect('example.com').to_not have_dns.with_type('A')
        expect('example.com').to have_dns.with_type('AAAA')
          .and_address('2001:2:6C::430')
      end

      it 'can evalutate a CNAME record' do
        stub_records(['www.example.com 300 IN CNAME example.com'])

        expect('example.com').to have_dns.with_type('CNAME')
        expect('example.com').to_not have_dns.with_type('AAAA')
        expect('example.com').to have_dns.with_type('CNAME').and_name('www.example.com')
      end

      it 'can evalutate an MX record' do
        stub_records(['example.com. 7200 MX 40 mail.example.com.'])

        expect('example.com').to have_dns.with_type('MX')
        expect('example.com').to_not have_dns.with_type('CNAME')
        expect('example.com').to have_dns.with_type('MX').and_preference(40)
        expect('example.com').to have_dns.with_type('MX')
          .and_preference(40).and_exchange('mail.example.com')
        expect('example.com').to_not have_dns.with_type('MX')
          .and_preference(30).and_exchange('mail.example.com')
        expect('example.com').to_not have_dns.with_type('MX')
          .and_preference(40).and_exchange('example.com')
      end

      it 'can evalutate an NS record' do
        stub_records(['sub.example.com. 300 IN NS ns.sub.example.com.'])

        expect('example.com').to have_dns.with_type('NS')
        expect('example.com').to_not have_dns.with_type('MX')
        expect('example.com').to have_dns.with_type('NS').and_domainname('ns.sub.example.com')
        expect('example.com').to_not have_dns.with_type('NS').and_domainname('sub.example.com')<commit_msg>Change to use documentation IP addresses on RFC5737 and RFC3849<commit_after>require 'spec_helper'

def stub_records(strings)
  records = strings.map { |s| Dnsruby::RR.new_from_string(s) }
  resolver = Dnsruby::Resolver.new
  allow(Dnsruby::Resolver).to receive(:new) do
    yield if block_given?
    resolver
  end
  allow(resolver).to receive_message_chain(:query, :answer).and_return(records)
end

describe 'rspec-dns matchers' do

  describe '#have_dns' do
    context 'with a sigle record' do
      it 'can evalutate an A record' do
        stub_records(['example.com 86400 A 192.0.2.4'])

        expect('example.com').to have_dns.with_type('A')
        expect('example.com').to_not have_dns.with_type('TXT')
        expect('example.com').to have_dns.with_type('A').and_address('192.0.2.4')
      end

      it 'can evalutate a AAAA record' do
        stub_records(['example.com 86400 AAAA 2001:DB8:6c::430'])

        expect('example.com').to have_dns.with_type('AAAA')
        expect('example.com').to_not have_dns.with_type('A')
        expect('example.com').to have_dns.with_type('AAAA')
          .and_address('2001:DB8:6C::430')
      end

      it 'can evalutate a CNAME record' do
        stub_records(['www.example.com 300 IN CNAME example.com'])

        expect('example.com').to have_dns.with_type('CNAME')
        expect('example.com').to_not have_dns.with_type('AAAA')
        expect('example.com').to have_dns.with_type('CNAME').and_name('www.example.com')
      end

      it 'can evalutate an MX record' do
        stub_records(['example.com. 7200 MX 40 mail.example.com.'])

        expect('example.com').to have_dns.with_type('MX')
        expect('example.com').to_not have_dns.with_type('CNAME')
        expect('example.com').to have_dns.with_type('MX').and_preference(40)
        expect('example.com').to have_dns.with_type('MX')
          .and_preference(40).and_exchange('mail.example.com')
        expect('example.com').to_not have_dns.with_type('MX')
          .and_preference(30).and_exchange('mail.example.com')
        expect('example.com').to_not have_dns.with_type('MX')
          .and_preference(40).and_exchange('example.com')
      end

      it 'can evalutate an NS record' do
        stub_records(['sub.example.com. 300 IN NS ns.sub.example.com.'])

        expect('example.com').to have_dns.with_type('NS')
        expect('example.com').to_not have_dns.with_type('MX')
        expect('example.com').to have_dns.with_type('NS').and_domainname('ns.sub.example.com')
        expect('example.com').to_not have_dns.with_type('NS').and_domainname('sub.example.com')
{'fraction_non_alphanumeric': 0.15268358845185043, 'fraction_numerical': 0.027707808564231738, 'mean_word_length': 3.45, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 4, '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': '498513', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1759, 'n_words': 286}