<commit_before># frozen_string_literal: true

require "rom/struct_compiler"

RSpec.describe ROM::StructCompiler, "#call" do
  subject(:struct_compiler) { ROM::StructCompiler.new }

  def attr_ast(name, type, **_opts)
    [name, ROM::Types.const_get(type).to_ast, {alias: false, wrapped: false}]
  end

  let(:input) do
    [:users, [[:attribute, attr_ast(:id, :Integer)], [:attribute, attr_ast(:name, :String)]]]
  end

  context "ROM::Struct" do
    it "generates valid class name in the default namespace" do
      ast = [
        :statuses,
        [
          [:attribute, attr_ast(:id, :Integer)],
          [:attribute, attr_ast(:name, :String)]
        ]
      ]

      struct = struct_compiler[*ast, ROM::Struct]

      expect(struct.name).to eql("ROM::Struct::Status")
    end

    it "generates a struct for a given relation name and columns" do
      struct = struct_compiler[*input, ROM::Struct]

      user = struct.new(id: 1, name: "Jane")

      expect(user.id).to be(1)
      expect(user.name).to eql("Jane")

      expect(user[:id]).to be(1)
      expect(user[:name]).to eql("Jane")

      expect(Hash[user]).to eql(id: 1, name: "Jane")

      expect(user.inspect).to eql('#<ROM::Struct::User id=1 name="Jane">')
      expect(user.to_s).to match(/\A#<ROM::Struct::User:0x[0-9a-f]+>\z/)
    end

    it "stores struct in the cache" do
      expect(struct_compiler[*input, ROM::Struct]).to be(struct_compiler[*input, ROM::Struct])
    end
<commit_msg>[rubocop] address Style/HashConversion<commit_after># frozen_string_literal: true

require "rom/struct_compiler"

RSpec.describe ROM::StructCompiler, "#call" do
  subject(:struct_compiler) { ROM::StructCompiler.new }

  def attr_ast(name, type, **_opts)
    [name, ROM::Types.const_get(type).to_ast, {alias: false, wrapped: false}]
  end

  let(:input) do
    [:users, [[:attribute, attr_ast(:id, :Integer)], [:attribute, attr_ast(:name, :String)]]]
  end

  context "ROM::Struct" do
    it "generates valid class name in the default namespace" do
      ast = [
        :statuses,
        [
          [:attribute, attr_ast(:id, :Integer)],
          [:attribute, attr_ast(:name, :String)]
        ]
      ]

      struct = struct_compiler[*ast, ROM::Struct]

      expect(struct.name).to eql("ROM::Struct::Status")
    end

    it "generates a struct for a given relation name and columns" do
      struct = struct_compiler[*input, ROM::Struct]

      user = struct.new(id: 1, name: "Jane")

      expect(user.id).to be(1)
      expect(user.name).to eql("Jane")

      expect(user[:id]).to be(1)
      expect(user[:name]).to eql("Jane")

      expect(user.to_h).to eql(id: 1, name: "Jane")

      expect(user.inspect).to eql('#<ROM::Struct::User id=1 name="Jane">')
      expect(user.to_s).to match(/\A#<ROM::Struct::User:0x[0-9a-f]+>\z/)
    end

    it "stores struct in the cache" do
      expect(struct_compiler[*input, ROM::Struct]).to be(struct_compiler[*input, ROM::Struct])
    end

{'fraction_non_alphanumeric': 0.16711772665764546, 'fraction_numerical': 0.005412719891745603, 'mean_word_length': 3.406855439642325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5380871', 'n_tokens_mistral': 1051, 'n_tokens_neox': 977, 'n_words': 224}