# frozen_string_literal: true require "rails_helper" require ::File.expand_path("../../../lib/cornucopia/util/configured_report", File.dirname(__FILE__)) class SubTestClass @sub_basic_variable @sub_overridden_variable def initialize @sub_basic_variable = "sub basic variable" @sub_overridden_variable = "sub overridden variable" end def sub_overridden_variable "sub processed variable" end end class TestPoint < Struct.new(:x, :y) end class TestClass @basic_variable @overridden_variable @sub_class_variable @hash_variable @nil_variable @struct_variable def initialize @basic_variable = "basic variable" @overridden_variable = "overridden variable" @sub_class_variable = SubTestClass.new @hash_variable = { key_1: "value", "another key" => "another value" } @array_variable = [1, "b", :fred_sym] @nil_variable = nil @struct_variable = TestPoint.new(rand(0..1_000), rand(0..1_000)) end def overridden_variable "processed variable" end end describe Cornucopia::Util::ConfiguredReport do let(:simple_report) { Cornucopia::Util::ConfiguredReport.new } let(:test) { TestClass.new } around(:each) do |example| expect(File.directory?(Rails.root.join("cornucopia_report/"))).to be_falsey begin example.run ensure Cornucopia::Util::ReportBuilder.current_report.close if (Cornucopia::Util::ReportBuilder.class_variable_get("@@current_report")) FileUtils.rm_rf Rails.root.join("cornucopia_report/") end end describe "#get_instance_variable" do it "gets the instance variable" do expect(simple_report.get_instance_variable(test, :@basic_variable, "basic_variable")).to be == "basic variable" end it "gets the getter function if there is one" do expect(simple_report.get_instance_variable(test, :@overridden_variable, "overridden_variable")). to be == "processed variable" end end describe "#split_full_field_symbol" do it "splits a list by __" do expect(simple_report.split_full_field_symbol(:var1__var2__var3)).to be == [:var1, :var2, :var3] end it "splits a list by __, and can handle varaibles with _" do expect(simple_report.split_full_field_symbol(:var1___var2__var3)).to be == [:var1, :_var2, :var3] end it "splits a list by __, and can handle varaibles with __" do expect(simple_report.split_full_field_symbol(:var1____var2__var3)).to be == [:var1, :__var2, :var3] end it "splits a list by __, and can handle varaibles with ____" do expect(simple_report.split_full_field_symbol(:var1______var2__var3)).to be == [:var1, :____var2, :var3] end it "can handle arbitrary _ and depths" do results = [] rand(5..20).times do |index| sub_symbol = Faker::Lorem.word if rand(5) == 0 || index == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end results << sub_symbol.to_sym end expect(simple_report.split_full_field_symbol(results.join("__").to_sym)).to be == results end end describe "#split_field_symbols" do it "returns [] when passed nil" do expect(simple_report.split_field_symbols(nil)).to be == [] end it "calls #split_full_field_symbol for every element in an array" do results = [] source = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << { report_element: sub_results } source << sub_results.join("__") end expect(simple_report.split_field_symbols(source)).to be == results end end describe "#find_variable_in_set" do it "finds variables that are in the set" do results = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << { report_element: sub_results } end set_test = results.sample expect(simple_report.find_variable_in_set(results, set_test[:report_element][0..-2], set_test[:report_element][-1])). to be == set_test end it "does not find variables that are only partial matches" do results = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << { report_element: sub_results } end set_test = results.sample[:report_element] len = rand(0..set_test.length - 3) expect(simple_report.find_variable_in_set(results, set_test[0..len], set_test[len + 1])).to be_falsey end it "does not find variables that are not in the set" do results = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << { report_element: sub_results } end # There is a slim, but statistically unlikely chance that # there will be another item in results with the first set # of words identical, except for the last word. # I'll take this risk. set_test = results.sample[:report_element] begin test_var = Faker::Lorem.word end while test_var == set_test[-1] expect(simple_report.find_variable_in_set(results, set_test[0..-2], test_var)).to be_falsey end it "is used by #expand_variable_inline?" do results = [] source = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << sub_results source << sub_results.join("__") end set_test = results.sample simple_report.expand_inline_fields = source expect(simple_report.expand_variable_inline?(set_test[0..-2], set_test[-1])).to be_truthy end it "is used by #expand_variable?" do results = [] source = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << sub_results source << sub_results.join("__") end set_test = results.sample simple_report.expand_inline_fields = source expect(simple_report.expand_variable?(set_test[0..-2], set_test[-1])).to be_truthy end it "is used by #expand_variable?" do results = [] source = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << sub_results source << sub_results.join("__") end set_test = results.sample simple_report.expand_fields = source expect(simple_report.expand_variable?(set_test[0..-2], set_test[-1])).to be_truthy end it "is used by #exclude_variable?" do results = [] source = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << sub_results source << sub_results.join("__") end set_test = results.sample simple_report.exclude_fields = source expect(simple_report.exclude_variable?(set_test[0..-2], set_test[-1])).to be_truthy end it "supports wildcards as the last field to match" do results = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << { report_element: sub_results } end # There is a slim, but statistically unlikely chance that # there will be another item in results with the first set # of words identical, except for the last word. # I'll take this risk. rand_pos = rand(0..results.length - 1) set_test = results[rand_pos][:report_element].clone results[rand_pos][:report_element][-1] = "*".to_sym expect(simple_report.find_variable_in_set(results, set_test[0..-2], set_test[-1])).to be_truthy end it "supports wildcards as any field to match" do results = [] rand(5..20).times do sub_results = [] rand(5..20).times do sub_symbol = Faker::Lorem.word if rand(5) == 0 sub_symbol = (("_" * rand(1..10)) + sub_symbol) end sub_results << sub_symbol.to_sym end results << { report_element: sub_results } end # There is a slim, but statistically unlikely chance that # there will be another item in results with the first set # of words identical, except for the last word. # I'll take this risk. rand_pos = rand(0..results.length - 1) set_test = results[rand_pos][:report_element].clone results[rand_pos][:report_element][rand(0..results[rand_pos][:report_element].length - 1)] = "*".to_sym expect(simple_report.find_variable_in_set(results, set_test[0..-2], set_test[-1])).to be_truthy end end describe "#export_field_record" do it "doesn't crash if there is an exception" do string_val = "string" Cornucopia::Util::ReportTable.new do |report_table| allow(report_table).to receive(:write_stats).and_call_original expect(report_table).to receive(:write_stats).with(:string_val, "string", {}).and_raise(Exception, "This is an error") simple_report.export_field_record({ report_element: [:string_val] }, string_val, :string_val, report_table, 0, report_object_set: true) expect(report_table.full_table).to match /Configured Report Error/ expect(report_table.full_table).to match /This is an error/ end end it "takes a passed in parent" do string_val = "string" Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with(:string_val, "string", {}) simple_report.export_field_record({ report_element: [:string_val] }, string_val, :string_val, report_table, 0, report_object_set: true) end end it "takes a parent that is an instance variable" do Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with(:overridden_variable, "processed variable", {}) simple_report.export_field_record({ report_element: [:test, :overridden_variable] }, test, :test, report_table, 1) end end it "deals with the variable we're fetching being the instance variable" do Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with(:overridden_variable, "processed variable", {}) simple_report.export_field_record({ report_element: [:test, :overridden_variable] }, test, :test, report_table, 1) end end it "outputs the variable .to_s" do Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with(:overridden_variable, "processed variable", {}) simple_report.export_field_record({ report_element: [:test, :overridden_variable, :to_s] }, test, :test, report_table, 1) end end it "finds the parent object if it is a member function" do Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with(:overridden_variable, "processed variable", {}) simple_report.export_field_record({ report_element: [:test, :overridden_variable] }, test, :test, report_table, 1) end end it "finds the parent object if it is accessible via []" do hash = { my_hash_key: "hash key" } Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with(:my_hash_key, "hash key", {}) simple_report.export_field_record({ report_element: [:hash, :my_hash_key] }, hash, :hash, report_table, 1) end end it "finds the parent object if it is an array index expanded" do array = [1, 2, 3] Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with("0".to_sym, 1, {}) simple_report.export_field_record({ report_element: [:array, 0.to_s.to_sym] }, array, :array, report_table, 1, { expanded_field: true }) end end it "finds the parent object if it is an array index expanded inline and puts the right value out" do array = [1, 2, 3] simple_report.expand_inline_fields = [:array] Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with("array[0]", 1, {}) simple_report.export_field_record({ report_element: [:array, 0.to_s.to_sym] }, array, :array, report_table, 1) end end it "finds the parent object if it is an array index exported directly" do array = [1, 2, 3] Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with("array[0]", 1, {}) simple_report.export_field_record({ report_element: [:array, 0.to_s.to_sym] }, array, :array, report_table, 1) end end it "outputs an error if it cannot find the parent object" do Cornucopia::Util::ReportTable.new do |report_table| expect(report_table).to receive(:write_stats).with("ERROR", "Could not identify field: hash__my_hash_key while exporting hash__my_hash_key") simple_report.export_field_record({ report_element: [:hash, :my_hash_key] }, test, :hash, report_table, 1) end end it "expands leaf nodes" do report_result_table = nil Cornucopia::Util::ReportTable.new do |report_table| report_result_table = report_table simple_report.expand_fields = [:test] simple_report.export_field_record({ report_element: [:test] }, test, :test, report_table, 0, report_object_set: true) end expect(report_result_table.full_table).to match(/\>\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\ntest\n\\n@sub_class_variable\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nsub_basic_variable\n\sub basic variable\\nsub_overridden_variable\n\sub processed variable\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\narray_variable\n\\n0\n\1\n\\n1\n\b\\n2\n\:fred_sym\n\\nstruct_variable\n\\nx\n\#{test.instance_variable_get(:@struct_variable).x}\n\\ny\n\#{test.instance_variable_get(:@struct_variable).y}\n\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\narray_variable\n\\n0\n\1\n\\n1\n\b\\n2\n\:fred_sym\n\\nstruct_variable\n\\nx\n\#{test.instance_variable_get(:@struct_variable).x}\n\\ny\n\#{test.instance_variable_get(:@struct_variable).y}\n\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\ntest\n\\nSomething_Extra\n\A value\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\\ntest\n\\nbasic_variable\n\basic variable\\noverridden_variable\n\processed variable\\nhash_variable\n\\nkey_1\n\value\\nanother key\n\another value\/ end end end end