Class: MySQLExpectations::Matchers::HaveField
- Inherits:
-
Object
- Object
- MySQLExpectations::Matchers::HaveField
- Defined in:
- lib/mysql_expectations/matchers/table_have_field.rb
Overview
A matcher that checks if a MySQLDumpExpectations::Table has a field. Optionally, checks the field type and nullability
Instance Attribute Summary collapse
-
#expected_field_name ⇒ Object
readonly
Returns the value of attribute expected_field_name.
-
#expected_nullable ⇒ Object
readonly
Returns the value of attribute expected_nullable.
-
#expected_type ⇒ Object
readonly
Returns the value of attribute expected_type.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #actual_nullable ⇒ Object
- #actual_type ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
- #field_error(negated: false) ⇒ Object
- #field_match? ⇒ Boolean
-
#initialize(expected_field_name) ⇒ HaveField
constructor
A new instance of HaveField.
- #matches?(table) ⇒ Boolean
- #nullable_error ⇒ Object
- #nullable_match? ⇒ Boolean
- #of_type(expected_type) ⇒ Object
- #that_is_not_nullable ⇒ Object
- #that_is_nullable ⇒ Object
- #type_error ⇒ Object
- #type_match? ⇒ Boolean
Constructor Details
#initialize(expected_field_name) ⇒ HaveField
Returns a new instance of HaveField.
14 15 16 17 18 19 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 14 def initialize(expected_field_name) @expected_field_name = expected_field_name @expected_type = nil @expected_nullable = nil @table = nil end |
Instance Attribute Details
#expected_field_name ⇒ Object (readonly)
Returns the value of attribute expected_field_name.
11 12 13 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 11 def expected_field_name @expected_field_name end |
#expected_nullable ⇒ Object (readonly)
Returns the value of attribute expected_nullable.
12 13 14 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 12 def expected_nullable @expected_nullable end |
#expected_type ⇒ Object (readonly)
Returns the value of attribute expected_type.
11 12 13 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 11 def expected_type @expected_type end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
12 13 14 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 12 def table @table end |
Instance Method Details
#actual_nullable ⇒ Object
36 37 38 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 36 def actual_nullable table.field(expected_field_name).nullable? end |
#actual_type ⇒ Object
25 26 27 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 25 def actual_type table.field(expected_field_name).type end |
#description ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 52 def description description = "have field '#{expected_field_name}'" description << " of type '#{expected_type}'" if expected_type description << ' that is nullable' if expected_nullable == true description << ' that is not nullable' if expected_nullable == false description end |
#failure_message ⇒ Object
81 82 83 84 85 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 81 def return field_error unless field_match? return type_error unless type_match? return nullable_error unless nullable_match? end |
#failure_message_when_negated ⇒ Object
87 88 89 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 87 def field_error(negated: true) end |
#field_error(negated: false) ⇒ Object
60 61 62 63 64 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 60 def field_error(negated: false) "expected '#{table.name}' table" \ " #{negated ? 'not ' : ''}to "\ "have field '#{expected_field_name}'." end |
#field_match? ⇒ Boolean
21 22 23 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 21 def field_match? table.field?(expected_field_name) end |
#matches?(table) ⇒ Boolean
47 48 49 50 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 47 def matches?(table) @table = table field_match? && type_match? && nullable_match? end |
#nullable_error ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 71 def nullable_error = "expected '#{table.name}.#{expected_field_name}' field " << 'to be ' << 'not ' unless expected_nullable << 'nullable but it was ' << 'not ' unless actual_nullable << 'nullable.' end |
#nullable_match? ⇒ Boolean
40 41 42 43 44 45 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 40 def nullable_match? if field_match? && !expected_nullable.nil? return actual_nullable == expected_nullable end true end |
#of_type(expected_type) ⇒ Object
91 92 93 94 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 91 def of_type(expected_type) @expected_type = expected_type self end |
#that_is_not_nullable ⇒ Object
101 102 103 104 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 101 def that_is_not_nullable @expected_nullable = false self end |
#that_is_nullable ⇒ Object
96 97 98 99 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 96 def that_is_nullable @expected_nullable = true self end |
#type_error ⇒ Object
66 67 68 69 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 66 def type_error "expected '#{table.name}.#{expected_field_name}' field " \ "to be of type '#{expected_type}' but it was '#{actual_type}'" end |
#type_match? ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 29 def type_match? if field_match? && !expected_type.nil? return actual_type == expected_type end true end |