Skip to content

Commit

Permalink
Add core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tcd committed Jan 4, 2020
1 parent 4df93f9 commit b6c2fa3
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Style/DocumentationMethod:
Exclude:
- 'test/**/*.rb'

Layout/HashAlignment:
Exclude:
- 'test/**/*.rb'

# Don't leave calls to pry lying around.
Lint/Debugger:
Enabled: true
Expand Down Expand Up @@ -65,7 +69,6 @@ Style/NumericPredicate:
Naming/VariableNumber:
Enabled: false


# ==============================================================================
# Braces, Brackets, & Parens
# ==============================================================================
Expand Down
2 changes: 1 addition & 1 deletion lib/coolkit/core/parse_json_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ module Coolkit
# @param (see #parse)
# @return [Hash]
def self.parse_json_file(path, opts = {})
return JSON.parse(File.read(File.expand_path(path)), **opts)
return JSON.parse(File.read(File.expand_path(path)), opts)
end
end
2 changes: 1 addition & 1 deletion lib/coolkit/core/string/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.comment!(string, indent_empty_lines: true)
# @param indent_empty_lines [Boolean] (true)
# @return [String]
def self.comment(string, indent_empty_lines: true)
return string.dup.tap { |s| comment!(s, indent_empty_lines) }
return string.dup.tap { |s| comment!(s, indent_empty_lines: indent_empty_lines) }
end

end
2 changes: 1 addition & 1 deletion lib/coolkit/core/string/normalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.normalize!(string)
#
# @param string [String]
# @return [String]
def self.normalize_string(string)
def self.normalize(string)
return string.dup.tap { |s| normalize!(s) }
end

Expand Down
File renamed without changes.
69 changes: 69 additions & 0 deletions test/coolkit/core/array_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "test_helper"
require "coolkit/core/remove_first_from_array"

module CoreTest
class ArrayTest < TestCase

# @return [Array]
def cases
return [
{
description: "remove string",
array: ["one", "two", "three"],
item: "two",
result: ["one", "three"],
},
{
description: "remove empty string",
array: ["one", "two", "", "three"],
item: "",
result: ["one", "two", "three"],
},
{
description: "remove number",
array: [0, 1, 2, 4],
item: 2,
result: [0, 1, 4],
},
{
description: "remove only the first occurrence of a number",
array: [0, 1, 2, 2, 4],
item: 2,
result: [0, 1, 2, 4],
},
{
description: "remove symbol from mixed array",
array: [1, "two", :three, "three"],
item: :three,
result: [1, "two", "three"],
},
{
description: "remove string from an empty array",
array: [],
item: "value",
result: [],
},
{
description: "remove nil from an empty array",
array: [],
item: nil,
result: [],
},
{
description: "remove item not present in array",
array: ["one", "two"],
item: "three",
result: ["one", "two"],
},
]
end

def test_remove_first!
cases = self.cases()
cases.each do |c|
assert_equal(c[:result], Coolkit.remove_first_from_array(c[:array], c[:item]))
end
end

end
end
18 changes: 18 additions & 0 deletions test/coolkit/core/json_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "test_helper"
require "coolkit/core/parse_json_file"

module CoreTest
class JSONTest < TestCase

def test_read
data = Coolkit.parse_json_file(file_fixture("simple.json"))
assert_equal("value", data["key"])
end

def test_read_with_options
data = Coolkit.parse_json_file(file_fixture("simple.json"), symbolize_names: true)
assert_equal("value", data[:key])
end

end
end
35 changes: 35 additions & 0 deletions test/coolkit/core/string/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "test_helper"
require "coolkit/core/string/comment"

module CoreTest
module StringTest
class CommentTest < TestCase

def test_comment_1
want = <<~STRING
# def some_method
# some_code
# end
STRING
input = <<~STRING
def some_method
some_code
end
STRING
assert_equal(want, Coolkit.comment(input))
end

def test_comment_2
assert_equal("# foo", Coolkit.comment("foo"))
assert_equal("# foo", Coolkit.comment(" foo"))
assert_equal("# foo\n# \t\tbar", Coolkit.comment("foo\n\t\tbar"))
end

def test_comment_3
assert_equal("# foo\n# \n# bar", Coolkit.comment("foo\n\nbar"))
assert_equal("# foo\n\n# bar", Coolkit.comment("foo\n\nbar", indent_empty_lines: false))
end

end
end
end
35 changes: 35 additions & 0 deletions test/coolkit/core/string/indent_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "test_helper"
require "coolkit/core/string/indent"

module CoreTest
module StringTest
class IndentTest < TestCase

def test_indent_1
want = <<-STRING
def some_method
some_code
end
STRING
input = <<~STRING
def some_method
some_code
end
STRING
assert_equal(want, Coolkit.indent(input, 2))
end

def test_indent_2
assert_equal(" foo", Coolkit.indent(" foo", 2))
assert_equal("\t\tfoo\n\t\t\t\tbar", Coolkit.indent("foo\n\t\tbar", 2))
assert_equal("\t\tfoo", Coolkit.indent("foo", 2, indent_string: "\t"))
end

def test_indent_3
assert_equal(" foo\n\n bar", Coolkit.indent("foo\n\nbar", 2))
assert_equal(" foo\n \n bar", Coolkit.indent("foo\n\nbar", 2, indent_string: nil, indent_empty_lines: true))
end

end
end
end
79 changes: 79 additions & 0 deletions test/coolkit/core/string/normalize_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require "test_helper"
require "coolkit/core/string/normalize"

module CoreTest
module StringTest
class NormalizeTest < TestCase
[
{
want: "Number of Accepted Transaction Sets",
have: "Number of Accepted Transaction Sets",
},
{
want: "Free Form Message",
have: "Free Form Message",
},
{
want: "Free Form Message",
have: "Free-Form Message",
},
{
want: "Pick up Date",
have: "Pick-up Date",
},
{
want: "Harbor Maintenance Fee Exemption Code",
have: "Harbor Maintenance Fee (HMF) Exemption Code",
},
{
want: "Product Service Substitution Code",
have: "Product/Service Substitution Code",
},
{
want: "Bill of Lading Waybill Number",
have: "Bill of Lading/Waybill Number",
},
{
want: "Pre Cooled Code",
have: "Pre-Cooled (Rule 710) Code",
},
{
want: "Surface Layer Position Code",
have: "Surface/Layer/Position Code",
},
{
want: "U P C EAN Consumer Package Code",
have: "U.P.C./EAN Consumer Package Code",
},
{
want: "D U N S Number",
have: "D-U-N-S Number",
},
{
want: "Service Promotion Allowance or Charge Code",
have: "Service, Promotion, Allowance, or Charge Code",
},
{
want: "Shippers Export Declaration Requirements",
have: "Shipper's Export Declaration Requirements",
},
{
want: "Resource Code",
have: "Resource Code (or Identifier)",
},
{
want: "Interchange ID Qualifier",
have: "Interchange ID Qualifier",
},
{
want: "Length of Binary Data",
have: "Length of Binary Data",
},
].each do |c|
define_method("test_normalize_#{c[:have]}") do
assert_equal(c[:want], Coolkit.normalize(c[:have]))
end
end
end
end
end
31 changes: 31 additions & 0 deletions test/coolkit/core/string/pascal_case_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "test_helper"
require "coolkit/core/string/pascal_case"

module CoreTest
module StringTest
class PascalCaseTest < TestCase
{
"Number of Accepted Transaction Sets" => "NumberOfAcceptedTransactionSets",
"Free Form Message" => "FreeFormMessage",
"Free-Form Message" => "FreeFormMessage",
"Pick-up Date" => "PickUpDate",
"Harbor Maintenance Fee (HMF) Exemption Code" => "HarborMaintenanceFeeExemptionCode",
"Product/Service Substitution Code" => "ProductServiceSubstitutionCode",
"Bill of Lading/Waybill Number" => "BillOfLadingWaybillNumber",
"Pre-Cooled (Rule 710) Code" => "PreCooledCode",
"Surface/Layer/Position Code" => "SurfaceLayerPositionCode",
"U.P.C./EAN Consumer Package Code" => "UPCEanConsumerPackageCode",
"D-U-N-S Number" => "DUNSNumber",
"Service, Promotion, Allowance, or Charge Code" => "ServicePromotionAllowanceOrChargeCode",
"Shipper's Export Declaration Requirements" => "ShippersExportDeclarationRequirements",
"Resource Code (or Identifier)" => "ResourceCode",
"Interchange ID Qualifier" => "InterchangeIdQualifier",
"Length of Binary Data" => "LengthOfBinaryData",
}.each do |input, want|
define_method("test_normalize_#{input}") do
assert_equal(want, Coolkit.pascal_case(input))
end
end
end
end
end
31 changes: 31 additions & 0 deletions test/coolkit/core/string/snake_case_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "test_helper"
require "coolkit/core/string/snake_case"

module CoreTest
module StringTest
class SnakeCaseTest < TestCase
{
"Number of Accepted Transaction Sets" => "number_of_accepted_transaction_sets",
"Free Form Message" => "free_form_message",
"Free-Form Message" => "free_form_message",
"Pick-up Date" => "pick_up_date",
"Harbor Maintenance Fee (HMF) Exemption Code" => "harbor_maintenance_fee_exemption_code",
"Product/Service Substitution Code" => "product_service_substitution_code",
"Bill of Lading/Waybill Number" => "bill_of_lading_waybill_number",
"Pre-Cooled (Rule 710) Code" => "pre_cooled_code",
"Surface/Layer/Position Code" => "surface_layer_position_code",
"U.P.C./EAN Consumer Package Code" => "upc_ean_consumer_package_code",
"D-U-N-S Number" => "d_u_n_s_number", # TODO: Don't like this result.
"Service, Promotion, Allowance, or Charge Code" => "service_promotion_allowance_or_charge_code",
"Shipper's Export Declaration Requirements" => "shippers_export_declaration_requirements",
"Resource Code (or Identifier)" => "resource_code",
"Interchange ID Qualifier" => "interchange_id_qualifier",
"Length of Binary Data" => "length_of_binary_data",
}.each do |input, want|
define_method("test_normalize_#{input}") do
assert_equal(want, Coolkit.snake_case(input))
end
end
end
end
end
34 changes: 34 additions & 0 deletions test/coolkit/core/string/trim_trailing_whitespace_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "test_helper"
require "coolkit/core/string/trim_trailing_whitespace"

module CoreTest
module StringTest
class TrimTrailingWhitespaceTest < TestCase

def test_1
want = "Hello\nWorld\n"
have = Coolkit.trim_trailing_whitespace("Hello \nWorld\n")
assert_equal(want, have)
end

def test_2
want = "Hello\n World\n"
have = Coolkit.trim_trailing_whitespace("Hello \n World\n")
assert_equal(want, have)
end

def test_3
want = "Hello\n\n World\n"
have = Coolkit.trim_trailing_whitespace("Hello \n\n World\n ")
assert_equal(want, have)
end

def test_4
want = "Hello\n\n World"
have = Coolkit.trim_trailing_whitespace("Hello \n\n World")
assert_equal(want, have)
end

end
end
end
File renamed without changes.
File renamed without changes.

0 comments on commit b6c2fa3

Please sign in to comment.