-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ohbarye/char-and-string-arbitrary
Char and string arbitrary
- Loading branch information
Showing
7 changed files
with
291 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# For available configuration options, see: | ||
# https://github.com/standardrb/standard | ||
ruby_version: 3.3 | ||
ruby_version: 3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbt | ||
module Arbitrary | ||
class CharArbitrary | ||
def initialize | ||
@arb = ChooseArbitrary.new(0..0x10FFFF) | ||
end | ||
|
||
# @return [String] | ||
def generate(rng) | ||
map(@arb.generate(rng)) | ||
end | ||
|
||
# Shrinks towards characters with lower codepoints, e.g. ASCII | ||
# @return [Enumerator] | ||
def shrink(current) | ||
Enumerator.new do |y| | ||
@arb.shrink(unmap(current)).each do |v| | ||
y << map(v) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def map(v) | ||
[v].pack("U") | ||
end | ||
|
||
def unmap(v) | ||
v.unpack1("U") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbt | ||
module Arbitrary | ||
ALPHANUMERIC_CHARS = [*("a".."z"), *("A".."Z"), *("0".."9")].freeze | ||
PRINTABLE_ASCII_CHARS = [*(" ".."~")].freeze | ||
ASCII_CHARS = [*PRINTABLE_ASCII_CHARS, "\n", "\r", "\t", "\v", "\b", "\f", "\e", "\d", "\a"].freeze | ||
PRINTABLE_CHARS = [ | ||
*ASCII_CHARS, | ||
*("\u{A0}".."\u{D7FF}"), | ||
*("\u{E000}".."\u{FFFD}"), | ||
*("\u{10000}".."\u{10FFFF}") | ||
].freeze | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbt | ||
module Arbitrary | ||
class StringArbitrary | ||
# @param arb [ArrayArbitrary] | ||
def initialize(arb) | ||
@arb = arb | ||
end | ||
|
||
# @return [Array] | ||
def generate(rng) | ||
map(@arb.generate(rng)) | ||
end | ||
|
||
# @return [Enumerator] | ||
def shrink(current) | ||
Enumerator.new do |y| | ||
@arb.shrink(unmap(current)).each do |v| | ||
y.yield map(v) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def map(v) | ||
v.join | ||
end | ||
|
||
def unmap(v) | ||
v.chars | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Pbt::Arbitrary::ArbitraryMethods do | ||
describe ".alphanumeric_char" do | ||
it "generates a character" do | ||
val = Pbt.alphanumeric_char.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to eq(1) | ||
end | ||
|
||
it "is alphanumeric char" do | ||
val = Pbt.alphanumeric_char.generate(Random.new) | ||
expect(Pbt::Arbitrary::ALPHANUMERIC_CHARS).to include(val) | ||
end | ||
end | ||
|
||
describe ".alphanumeric_string" do | ||
it "generates a string" do | ||
arb = Pbt.alphanumeric_string(min: 1, max: 5) | ||
aggregate_failures do | ||
100.times do | ||
val = arb.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to be >= 1 | ||
expect(val.size).to be <= 5 | ||
end | ||
end | ||
end | ||
|
||
it "is alphanumeric string" do | ||
val = Pbt.alphanumeric_string.generate(Random.new) | ||
val.chars.each do |char| | ||
expect(Pbt::Arbitrary::ALPHANUMERIC_CHARS).to include(char) | ||
end | ||
end | ||
end | ||
|
||
describe ".ascii_char" do | ||
it "generates a character" do | ||
val = Pbt.ascii_char.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to eq(1) | ||
end | ||
|
||
it "is printable ascii char" do | ||
val = Pbt.ascii_char.generate(Random.new) | ||
expect(Pbt::Arbitrary::ASCII_CHARS).to include(val) | ||
end | ||
end | ||
|
||
describe ".ascii_string" do | ||
it "generates a string" do | ||
arb = Pbt.ascii_string(min: 1, max: 5) | ||
aggregate_failures do | ||
100.times do | ||
val = arb.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to be >= 1 | ||
expect(val.size).to be <= 5 | ||
end | ||
end | ||
end | ||
|
||
it "is ascii string" do | ||
val = Pbt.ascii_string.generate(Random.new) | ||
val.chars.each do |char| | ||
expect(Pbt::Arbitrary::ASCII_CHARS).to include(char) | ||
end | ||
end | ||
end | ||
|
||
describe ".printable_ascii_char" do | ||
it "generates a character" do | ||
val = Pbt.printable_ascii_char.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to eq(1) | ||
end | ||
|
||
it "is printable ascii char" do | ||
val = Pbt.printable_ascii_char.generate(Random.new) | ||
expect(Pbt::Arbitrary::PRINTABLE_ASCII_CHARS).to include(val) | ||
end | ||
end | ||
|
||
describe ".printable_ascii_string" do | ||
it "generates a string" do | ||
arb = Pbt.printable_ascii_string(min: 1, max: 5) | ||
aggregate_failures do | ||
100.times do | ||
val = arb.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to be >= 1 | ||
expect(val.size).to be <= 5 | ||
end | ||
end | ||
end | ||
|
||
it "is printable_ascii string" do | ||
val = Pbt.printable_ascii_string.generate(Random.new) | ||
val.chars.each do |char| | ||
expect(Pbt::Arbitrary::PRINTABLE_ASCII_CHARS).to include(char) | ||
end | ||
end | ||
end | ||
|
||
describe ".printable_char" do | ||
it "generates a character" do | ||
val = Pbt.printable_char.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to eq(1) | ||
end | ||
|
||
it "is printable char" do | ||
val = Pbt.printable_char.generate(Random.new) | ||
expect(Pbt::Arbitrary::PRINTABLE_CHARS).to include(val) | ||
end | ||
end | ||
|
||
describe ".printable_string" do | ||
it "generates a string" do | ||
arb = Pbt.printable_string(min: 1, max: 5) | ||
aggregate_failures do | ||
100.times do | ||
val = arb.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to be >= 1 | ||
expect(val.size).to be <= 5 | ||
end | ||
end | ||
end | ||
|
||
it "is printable string" do | ||
val = Pbt.printable_string.generate(Random.new) | ||
val.chars.each do |char| | ||
expect(Pbt::Arbitrary::PRINTABLE_CHARS).to include(char) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Pbt::Arbitrary::CharArbitrary do | ||
describe "#generate" do | ||
it "generates an character" do | ||
val = Pbt::Arbitrary::CharArbitrary.new.generate(Random.new) | ||
expect(val).to be_a(String) | ||
expect(val.size).to eq(1) | ||
end | ||
end | ||
|
||
describe "#shrink" do | ||
it "returns an Enumerator" do | ||
arb = Pbt::Arbitrary::CharArbitrary.new | ||
val = arb.generate(Random.new) | ||
expect(arb.shrink(val)).to be_a(Enumerator) | ||
end | ||
|
||
it "returns an Enumerator that iterates characters shrinking towards lower codepoint" do | ||
arb = Pbt::Arbitrary::CharArbitrary.new | ||
expect(arb.shrink("z").to_a).to eq ["=", "\u001F", "\u0010", "\b", "\u0004", "\u0002", "\u0001", "\u0000"] | ||
end | ||
end | ||
end |