Class: RailsProof::AiTestIdentity

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_proof/ai_test_identity.rb

Class Method Summary collapse

Class Method Details

.assertion_keys(test_code) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_proof/ai_test_identity.rb', line 34

def assertion_keys(test_code)
  return [] unless test_code.is_a?(String)
  return [] if test_code.strip.empty?

  test_code.each_line.filter_map do |line|
    assertion = normalize_assertion(line)

    next unless assertion
    next if trivial_assertion?(assertion)

    assertion
  end.uniq
end

.behavior_keys(test_code) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rails_proof/ai_test_identity.rb', line 48

def behavior_keys(test_code)
  return [] unless test_code.is_a?(String)
  return [] if test_code.strip.empty?

  context = []
  keys = []

  test_code.each_line do |line|
    normalized = normalize_code_line(line)

    next if normalized.empty?
    next if normalized.start_with?("#")
    next if test_declaration?(normalized)
    next if normalized == "end"

    assertion = normalize_assertion(normalized)

    if assertion
      unless trivial_assertion?(assertion)
        keys << behavior_key(
          context: context,
          assertion: assertion
        )
      end

      next
    end

    context << normalized
  end

  keys.uniq
end

.name_keys(name:, test_code:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails_proof/ai_test_identity.rb', line 8

def name_keys(name:, test_code:)
  names = [name]

  if test_code.is_a?(String)
    inspector = RailsProof::TestInspector.new(test_code)

    names.concat(
      inspector.test_cases.map do |test_case|
        test_case[:name]
      end
    )
  end

  names
    .filter_map { |value| normalize_name(value) }
    .uniq
end

.normalize_name(value) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/rails_proof/ai_test_identity.rb', line 120

def normalize_name(value)
  normalized = value
    .to_s
    .downcase
    .gsub(/[^a-z0-9]+/, " ")
    .strip
    .squeeze(" ")

  normalized.empty? ? nil : normalized
end

.same?(first_name:, first_test_code:, second_name:, second_test_code:) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rails_proof/ai_test_identity.rb', line 82

def same?(
  first_name:,
  first_test_code:,
  second_name:,
  second_test_code:
)
  return true if same_name?(
    first_name: first_name,
    first_test_code: first_test_code,
    second_name: second_name,
    second_test_code: second_test_code
  )

  same_meaningful_behavior?(
    first_test_code: first_test_code,
    second_test_code: second_test_code
  )
end

.same_candidate?(first_name:, first_test_code:, second_name:, second_test_code:) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rails_proof/ai_test_identity.rb', line 101

def same_candidate?(
  first_name:,
  first_test_code:,
  second_name:,
  second_test_code:
)
  return true if same?(
    first_name: first_name,
    first_test_code: first_test_code,
    second_name: second_name,
    second_test_code: second_test_code
  )

  same_test_body?(
    first_test_code,
    second_test_code
  )
end

.test_fingerprint(test_code) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rails_proof/ai_test_identity.rb', line 26

def test_fingerprint(test_code)
  canonical = canonical_test_code(test_code)

  return nil unless canonical

  Digest::SHA256.hexdigest(canonical)
end