From a84e1c17a4325d90118c636092aef0f255c93c9b Mon Sep 17 00:00:00 2001 From: Ben Mills Date: Thu, 31 Mar 2016 19:55:27 -0600 Subject: [PATCH] Cover more with tests --- .../transform_test.rb | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 test/active_model_serializers/transform_test.rb diff --git a/test/active_model_serializers/transform_test.rb b/test/active_model_serializers/transform_test.rb new file mode 100644 index 000000000..ee2e460fb --- /dev/null +++ b/test/active_model_serializers/transform_test.rb @@ -0,0 +1,123 @@ +require 'test_helper' + +class ActiveModelSerializers::TransformTest < ActiveSupport::TestCase + def test_camel + obj = Object.new + scenarios = [ + { + value: { :some_key => 'value' }, + expected: { :SomeKey => 'value' } + }, + { + value: { 'some_key' => 'value' }, + expected: { 'SomeKey' => 'value' } + }, + { + value: :some_value, + expected: :SomeValue + }, + { + value: obj, + expected: obj + }, + { + value: nil, + expected: nil + } + ] + scenarios.each do |s| + result = ActiveModelSerializers::Transform.camel(s[:value]) + assert_equal s[:expected], result + end + end + + def test_camel_lower + obj = Object.new + scenarios = [ + { + value: { :some_key => 'value' }, + expected: { :someKey => 'value' } + }, + { + value: { 'some_key' => 'value' }, + expected: { 'someKey' => 'value' } + }, + { + value: :some_value, + expected: :someValue + }, + { + value: obj, + expected: obj + }, + { + value: nil, + expected: nil + } + ] + scenarios.each do |s| + result = ActiveModelSerializers::Transform.camel_lower(s[:value]) + assert_equal s[:expected], result + end + end + + def test_dash + obj = Object.new + scenarios = [ + { + value: { :some_key => 'value' }, + expected: { :"some-key" => 'value' } + }, + { + value: { 'some_key' => 'value' }, + expected: { 'some-key' => 'value' } + }, + { + value: :some_value, + expected: :"some-value" + }, + { + value: obj, + expected: obj + }, + { + value: nil, + expected: nil + } + ] + scenarios.each do |s| + result = ActiveModelSerializers::Transform.dash(s[:value]) + assert_equal s[:expected], result + end + end + + def test_underscore + obj = Object.new + scenarios = [ + { + value: { :"some-key" => 'value' }, + expected: { :some_key => 'value' } + }, + { + value: { 'some-key' => 'value' }, + expected: { 'some_key' => 'value' } + }, + { + value: :"some-value", + expected: :some_value + }, + { + value: obj, + expected: obj + }, + { + value: nil, + expected: nil + } + ] + scenarios.each do |s| + result = ActiveModelSerializers::Transform.underscore(s[:value]) + assert_equal s[:expected], result + end + end +end \ No newline at end of file