Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement string_to_not_be_blank assertion #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added `expect/string_to_not_be_blank` for asserting that a string is neither empty nor consists only of whitespace characters.

## [0.5.0] - 2024-07-31

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 1.1.5
title: expect/string_to_not_be_blank given empty string
---
Expected "" to not be blank

- Expected
+ Received

- non-empty or non-whitespace-only string
+ ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
version: 1.1.5
title: expect/string_to_not_be_blank given whitespace-only string
---
Expected "\t \n " to not be blank

- Expected
+ Received

- non-empty or non-whitespace-only string
+ "\t \n "
14 changes: 14 additions & 0 deletions src/startest/expect.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ pub fn to_be_none(actual: Option(a)) -> Nil {
}
}

/// Asserts that the given string is neither empty nor consists only of whitespace characters.
pub fn string_to_not_be_blank(actual: String) -> Nil {
case actual |> string.trim() |> string.is_empty() {
False -> Nil
True ->
AssertionError(
string.concat(["Expected ", string.inspect(actual), " to not be blank"]),
string.inspect(actual),
"non-empty or non-whitespace-only string",
)
|> assertion_error.raise
}
}

/// Asserts that the given string contains the expected string.
pub fn string_to_contain(actual: String, expected: String) -> Nil {
case string.contains(does: actual, contain: expected) {
Expand Down
22 changes: 22 additions & 0 deletions test/startest/expect_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ pub fn to_be_none_tests() {
])
}

pub fn string_to_not_be_blank_tests() {
describe("startest/expect", [
describe("string_to_not_be_blank", [
describe("given a string that is neither empty nor whitespace-only", [
it_passes(fn() { expect.string_to_not_be_blank("Gunter") }),
]),
describe("given a string that is empty", [
it_fails_matching_snapshot(
"expect/string_to_not_be_blank given empty string",
fn() { expect.string_to_not_be_blank("") },
),
]),
describe("given a string that consists only of whitespace characters", [
it_fails_matching_snapshot(
"expect/string_to_not_be_blank given whitespace-only string",
fn() { expect.string_to_not_be_blank("\t \n ") },
),
]),
]),
])
}

pub fn string_to_contain_tests() {
describe("startest/expect", [
describe("string_to_contain", [
Expand Down
Loading