require 'spec_helper' require 'flydata/json/json_ext' describe JSON do describe '.generate_kv_pairs' do subject { described_class.generate_kv_pairs(keys, values) } context 'testing values' do let(:keys) { ['test'] } let(:values) { [ value ] } let(:expected_result) { %Q|{"test":"#{expected_value}"}| } shared_examples 'producing the expected result' do it do is_expected.to eq expected_result end end context 'regular string' do let(:value) { "a regular text" } let(:expected_value) { value } it_behaves_like "producing the expected result" end context 'regular UTF-8 2 byte chars' do let(:value) { "¢Àø" } let(:expected_value) { value } it_behaves_like "producing the expected result" end context 'regular UTF-8 3 byte chars' do let(:value) { "三国一" } let(:expected_value) { value } it_behaves_like "producing the expected result" end context '/' do let(:value) { "/" } let(:expected_value) { "\\/" } it_behaves_like "producing the expected result" end context '\\' do let(:value) { "\\" } let(:expected_value) { "\\\\" } it_behaves_like "producing the expected result" end context '"' do let(:value) { "\"" } let(:expected_value) { "\\\"" } it_behaves_like "producing the expected result" end context '\'' do let(:value) { "'" } let(:expected_value) { "'" } it_behaves_like "producing the expected result" end context '\b' do let(:value) { "\b" } let(:expected_value) { "\\b" } it_behaves_like "producing the expected result" end context '\x02' do let(:value) { "\x02" } let(:expected_value) { "\\u0002" } it_behaves_like "producing the expected result" end context '^\\' do let(:value) { "\x1c" } let(:expected_value) { "\\u001c" } it_behaves_like "producing the expected result" end end context 'testing memory' do let(:keys) { ['id', 'user_id', 'name', 'timestamp'] } let(:values) { ['29388381012', '192938439', 'Muy Fiel y Reconquistadora Ciudad de San Felipe y Santiago de Montevideo', '2015-12-01 12:34:56' ] } let(:expected_result) { %Q|{"id":"29388381012","user_id":"192938439","name":"Muy Fiel y Reconquistadora Ciudad de San Felipe y Santiago de Montevideo","timestamp":"2015-12-01 12:34:56"}| } xcontext 'when run many many times' do thread = nil before do GC.stress = true thread = Thread.new { loop {rand(9).to_s * rand(9)} } end it do 6000.times do result = described_class.generate_kv_pairs(keys, values) rand(9).to_s * rand(9) expect(result).to eq expected_result end end after do GC.stress = false thread.kill end end end end end