Skip to content

Commit

Permalink
Publish the assert_prints spec helper (crystal-lang#13599)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
  • Loading branch information
2 people authored and Blacksmoke16 committed Dec 11, 2023
1 parent 2b0543b commit 31a4456
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 111 deletions.
26 changes: 2 additions & 24 deletions spec/manual/string_normalize_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "http/client"
require "../support/string"
require "spec/helpers/string"

UCD_ROOT = "http://www.unicode.org/Public/#{Unicode::VERSION}/ucd/"

Expand All @@ -27,31 +27,9 @@ private struct CodepointsEqualExpectation
end
end

# same as `assert_prints`, but uses `CodepointsEqualExpectation` instead of `eq`
private macro assert_prints_codepoints(call, str, desc, *, file = __FILE__, line = __LINE__)
%expectation = CodepointsEqualExpectation.new(({{ str }}).as(String), {{ desc }})

%result = {{ call }}
%result.should be_a(String), file: {{ file }}, line: {{ line }}
%result.should %expectation, file: {{ file }}, line: {{ line }}

String.build do |io|
{% if call.receiver %}{{ call.receiver }}.{% end %}{{ call.name }}(
io,
{% for arg in call.args %} {{ arg }}, {% end %}
{% if call.named_args %} {% for narg in call.named_args %} {{ narg.name }}: {{ narg.value }}, {% end %} {% end %}
) {{ call.block }}
end.should %expectation, file: {{ file }}, line: {{ line }}

{% unless flag?(:without_iconv) %}
string_build_via_utf16 do |io|
{% if call.receiver %}{{ call.receiver }}.{% end %}{{ call.name }}(
io,
{% for arg in call.args %} {{ arg }}, {% end %}
{% if call.named_args %} {% for narg in call.named_args %} {{ narg.name }}: {{ narg.value }}, {% end %} {% end %}
) {{ call.block }}
end.should %expectation, file: {{ file }}, line: {{ line }}
{% end %}
assert_prints({{ call }}, should: %expectation, file: {{ file }}, line: {{ line }})
end

private def assert_normalized(source, target, form : Unicode::NormalizationForm, *, file = __FILE__, line = __LINE__)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/base64_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec"
require "base64"
require "crystal/digest/md5"
require "../support/string"
require "spec/helpers/string"

# rearrange parameters for `assert_prints`
{% for method in %w(encode strict_encode urlsafe_encode) %}
Expand Down
2 changes: 1 addition & 1 deletion spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "big"
require "../../support/string"
require "spec/helpers/string"

describe BigDecimal do
it "initializes from valid input" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/big/big_float_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "big"
require "../../support/string"
require "spec/helpers/string"

private def it_converts_to_s(value : BigFloat, str, *, file = __FILE__, line = __LINE__)
it "converts to #{str}", file: file, line: line do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/char_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec"
require "unicode"
require "spec/helpers/iterate"
require "../support/string"
require "spec/helpers/string"

describe "Char" do
describe "#upcase" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/csv/csv_build_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "csv"
require "../../support/string"
require "spec/helpers/string"

describe CSV do
describe "build" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/enum_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "../support/string"
require "spec/helpers/string"

enum SpecEnum : Int8
One
Expand Down
2 changes: 1 addition & 1 deletion spec/std/float_printer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

require "spec"
require "./spec_helper"
require "../support/string"
require "spec/helpers/string"
require "../support/number"

# Tests that `v.to_s` is the same as the *v* literal is written in the source
Expand Down
2 changes: 1 addition & 1 deletion spec/std/float_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "../support/string"
require "spec/helpers/string"

describe "Float" do
describe "**" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/http/http_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "http"
require "../../support/string"
require "spec/helpers/string"

private def http_quote_string(io : IO, string)
HTTP.quote_string(string, io)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/humanize_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "../support/string"
require "spec/helpers/string"

private LENGTH_UNITS = ->(magnitude : Int32, number : Float64) do
case magnitude
Expand Down
2 changes: 1 addition & 1 deletion spec/std/json/builder_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "json"
require "../../support/string"
require "spec/helpers/string"

private def assert_built(expected, *, file = __FILE__, line = __LINE__, &)
assert_prints JSON.build { |json| with json yield json }, expected, file: file, line: line
Expand Down
2 changes: 1 addition & 1 deletion spec/std/mime/media_type_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../spec_helper"
require "mime/media_type"
require "../../support/string"
require "spec/helpers/string"

private def parse(string)
type = MIME::MediaType.parse(string)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/process/status_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "../../support/string"
require "spec/helpers/string"

private def exit_status(status)
{% if flag?(:unix) %}
Expand Down
2 changes: 1 addition & 1 deletion spec/std/slice_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "spec/helpers/iterate"
require "../support/string"
require "spec/helpers/string"

private class BadSortingClass
include Comparable(self)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/socket/address_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec"
require "socket"
require "../../support/win32"
require "../../support/string"
require "spec/helpers/string"

describe Socket::Address do
describe ".parse" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/sprintf_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "./spec_helper"
require "../support/string"
require "spec/helpers/string"
require "big"

# use same name for `sprintf` and `IO#printf` so that `assert_prints` can be leveraged
Expand Down
11 changes: 7 additions & 4 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"
require "spec/helpers/iterate"
require "../support/string"
require "spec/helpers/string"

describe "String" do
describe "[]" do
Expand Down Expand Up @@ -692,7 +692,8 @@ describe "String" do
end

it "does not touch invalid code units in an otherwise ascii string" do
assert_prints "\xB5!\xE0\xC1\xB5?".capitalize, "\xB5!\xE0\xC1\xB5?"
"\xB5!\xE0\xC1\xB5?".capitalize.should eq("\xB5!\xE0\xC1\xB5?")
String.build { |io| "\xB5!\xE0\xC1\xB5?".capitalize(io) }.should eq("\xB5!\xE0\xC1\xB5?".scrub)
end
end

Expand All @@ -710,8 +711,10 @@ describe "String" do
end

it "does not touch invalid code units in an otherwise ascii string" do
assert_prints "\xB5!\xE0\xC1\xB5?".titleize, "\xB5!\xE0\xC1\xB5?"
assert_prints "a\xA0b".titleize, "A\xA0b"
"\xB5!\xE0\xC1\xB5?".titleize.should eq("\xB5!\xE0\xC1\xB5?")
"a\xA0b".titleize.should eq("A\xA0b")
String.build { |io| "\xB5!\xE0\xC1\xB5?".titleize(io) }.should eq("\xB5!\xE0\xC1\xB5?".scrub)
String.build { |io| "a\xA0b".titleize(io) }.should eq("A\xA0b".scrub)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/time/format_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "./spec_helper"
require "../../support/string"
require "spec/helpers/string"

def parse_time(format, string)
Time.parse_utc(format, string)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/uri_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "spec"
require "uri"
require "uri/json"
require "uri/yaml"
require "../support/string"
require "spec/helpers/string"

private def assert_uri(string, file = __FILE__, line = __LINE__, **args)
it "`#{string}`", file, line do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/uuid_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "uuid"
require "../support/string"
require "spec/helpers/string"

describe "UUID" do
describe "#==" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/xml/builder_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "xml"
require "../../support/string"
require "spec/helpers/string"

private def assert_built(expected, quote_char = nil, *, file = __FILE__, line = __LINE__, &)
assert_prints XML.build(quote_char: quote_char) { |xml| with xml yield xml }, expected, file: file, line: line
Expand Down
2 changes: 1 addition & 1 deletion spec/std/xml/xml_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "xml"
require "../../support/string"
require "spec/helpers/string"

describe XML do
it "parses" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/yaml/builder_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "yaml"
require "../../support/string"
require "spec/helpers/string"

private def assert_built(expected, expect_document_end = false, *, file = __FILE__, line = __LINE__, &)
# libyaml 0.2.1 removed the erroneously written document end marker (`...`) after some scalars in root context (see https://github.com/yaml/libyaml/pull/18).
Expand Down
61 changes: 0 additions & 61 deletions spec/support/string.cr

This file was deleted.

Loading

0 comments on commit 31a4456

Please sign in to comment.