summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-08-01 22:10:53 +0900
committerGitHub <[email protected]>2019-08-01 22:10:53 +0900
commit3bacc302a3b3dd875de87d895cb9b532b522a4f0 (patch)
treecce84526455a3d70a79d919b318e0dcf36222e7f /test
parent3d3a6da2ca89a9f3bab01fdd79fed32c18b02005 (diff)
parenteea42e06af745f0a7ae68d0d364a8f71d72823fe (diff)
downloadmruby-3bacc302a3b3dd875de87d895cb9b532b522a4f0.tar.gz
mruby-3bacc302a3b3dd875de87d895cb9b532b522a4f0.zip
Merge pull request #4608 from shuujii/add-new-specifiers-modifiers-to-format-string-of-mrb_vfromat
Add new specifiers/modifiers to format string of `mrb_vfromat()`
Diffstat (limited to 'test')
-rw-r--r--test/t/vformat.rb88
1 files changed, 88 insertions, 0 deletions
diff --git a/test/t/vformat.rb b/test/t/vformat.rb
new file mode 100644
index 000000000..2270d6490
--- /dev/null
+++ b/test/t/vformat.rb
@@ -0,0 +1,88 @@
+def assert_format(exp, args)
+ assert_equal(exp, TestVFormat.format(*args))
+end
+
+def assert_format_pattern(exp_pattern, args)
+ assert_match(exp_pattern, TestVFormat.format(*args))
+end
+
+# Pass if ArgumentError is raised or return value is +exp+.
+def assert_implementation_dependent(exp, args)
+ begin
+ ret = TestVFormat.format(*args)
+ rescue ArgumentError
+ return pass
+ end
+ if ret == exp
+ pass
+ else
+ flunk "", "Expected ArgumentError is raised or #{ret.inspect} to be #{exp}."
+ end
+end
+
+def sclass(v)
+ class << v
+ self
+ end
+end
+
+assert('mrb_vformat') do
+ n = TestVFormat::Native
+ assert_format '', ['']
+ assert_format 'No specifier!', ['No specifier!']
+ assert_format '`c`: C', ['`c`: %c', n[?C]]
+ assert_format '`d`: 123', ['`d`: %d', n[123]]
+ assert_format '`i`: -83', ['`i`: %i', n[-83]]
+ assert_format '`f`: 0.0125', ['`f`: %f', n[0.0125]]
+ assert_format '`t`: NilClass', ['`t`: %t', nil]
+ assert_format '`t`: FalseClass', ['`t`: %t', false]
+ assert_format '`t`: TrueClass', ['`t`: %t', true]
+ assert_format '`t`: Fixnum', ['`t`: %t', 0]
+ assert_format '`t`: Hash', ['`t`: %t', k: "value"]
+ assert_format_pattern '#<Class:#<Class:#<Hash:0x*>>>', ['%t', sclass({})]
+ assert_format '-Infinity', ['%f', n[-Float::INFINITY]]
+ assert_format 'NaN: Not a Number', ['%f: Not a Number', n[Float::NAN]]
+ assert_format 'string and length', ['string %l length', n['andante'], n[3]]
+ assert_format '`n`: sym', ['`n`: %n', n[:sym]]
+ assert_format '%C文字列%', ['%s', n['%C文字列%']]
+ assert_format '`C`: Kernel module', ['`C`: %C module', n[Kernel]]
+ assert_format '`C`: NilClass', ['`C`: %C', n[nil.class]]
+ assert_format_pattern '#<Class:#<String:0x*>>', ['%C', n[sclass("")]]
+ assert_format '`T`: NilClass', ['`T`: %T', nil]
+ assert_format '`T`: FalseClass', ['`T`: %T', false]
+ assert_format '`T`: TrueClass', ['`T`: %T', true]
+ assert_format '`T`: Fixnum', ['`T`: %T', 0]
+ assert_format '`T`: Hash', ['`T`: %T', k: "value"]
+ assert_format_pattern 'Class', ['%T', sclass({})]
+ assert_format '`Y`: nil', ['`Y`: %Y', nil]
+ assert_format '`Y`: false', ['`Y`: %Y', false]
+ assert_format '`Y`: true', ['`Y`: %Y', true]
+ assert_format '`Y`: Fixnum', ['`Y`: %Y', 0]
+ assert_format '`Y`: Hash', ['`Y`: %Y', k: "value"]
+ assert_format 'Class', ['%Y', sclass({})]
+ assert_format_pattern '#<Class:#<String:0x*>>', ['%v', sclass("")]
+ assert_format '`v`: 1...3', ['`v`: %v', 1...3]
+ assert_format '`S`: {:a=>1, "b"=>"c"}', ['`S`: %S', a: 1, "b" => ?c]
+ assert_format 'percent: %', ['percent: %%']
+ assert_format '"I": inspect char', ['%!c: inspect char', n[?I]]
+ assert_format '709: inspect mrb_int', ['%!d: inspect mrb_int', n[709]]
+ assert_format '"a\x00b\xff"', ['%!l', n["a\000b\xFFc\000d"], n[4]]
+ assert_format ':"&.": inspect symbol', ['%!n: inspect symbol', n['&.'.to_sym]]
+ assert_format 'inspect "String"', ['inspect %!v', 'String']
+ assert_format 'inspect Array: [1, :x, {}]', ['inspect Array: %!v', [1,:x,{}]]
+ assert_format_pattern '`!C`: #<Class:0x*>', ['`!C`: %!C', n[Class.new]]
+ assert_format 'to_s -> to_s: ab,cd', ['to_s -> to_s: %n,%v', n[:ab], 'cd']
+ assert_format 'to_s -> inspect: x:y', ['to_s -> inspect: %v%!v', 'x', :y]
+ assert_format 'inspect -> to_s: "a"b', ['inspect -> to_s: %!v%n', 'a', n[:b]]
+ assert_format 'Y -> to_s: nile', ['Y -> to_s: %Y%v', nil, "e"]
+ assert_format '"abc":Z', ['%!s%!n', n['abc'], n[:Z]]
+ assert_format 'escape: \\%a,b,c,d', ['escape: \\\\\%a,b,\c%v', ',d']
+
+ assert_implementation_dependent 'unknown specifier: %^',
+ ['unknown specifier: %^']
+ assert_implementation_dependent 'unknown specifier with modifier: %!^',
+ ['unknown specifier with modifier: %!^']
+ assert_implementation_dependent 'termination is \\', ['termination is \\']
+ assert_implementation_dependent 'termination is %', ['termination is %']
+ assert_implementation_dependent 'termination is %!', ['termination is %!']
+end