본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리

    std.test

    Assert ordinary value conditions and compare output with existing golden text.

    이 모듈의 API

    9개 API

    이름모듈
    std.test.assert →std.test →
    std.test.assertContains →std.test →
    std.test.assertEq →std.test →
    std.test.assertErr →std.test →
    std.test.assertGolden →std.test →
    std.test.assertNe →std.test →
    std.test.assertNone →std.test →
    std.test.assertOk →std.test →
    std.test.assertSome →std.test →

    Module

    링크

    Successful assertions return unit or the requested payload without printing. Failed conditions raise TPZ4007 at the call and stop the current test. Golden assertions compare existing text without updating it. Use the language for loop to repeat assertions.

    Why

    링크

    Assertions use the same comparisons and constructors as ordinary programs. A test failure identifies the expectation without changing the data used to judge it.

    Availability

    링크

    Import std.test in language mode 5.21. The current compiler selects std/test.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Public API

    링크

    std.test.assert →

    std.test.assertEq →

    std.test.assertNe →

    std.test.assertContains →

    std.test.assertOk →

    std.test.assertErr →

    std.test.assertSome →

    std.test.assertNone →

    std.test.assertGolden →

    Pending contracts

    링크

    Repeat assertions with for

    링크
    규칙topaz.stdlib.contract.test-for-all.rule

    Repeat assertions with for

    Test programs iterate supplied values with the ordinary for statement. The same statement applies to integers, booleans, strings, and other iterable element types.

    std.test does not export forAllInt or forAllBool. Assertions inside a for body retain the same assertion behavior as a direct call.

    이유

    The removed helpers repeated an existing loop and added no test meaning. Ordinary iteration works for every element type and keeps loop control and assertion order visible.

    Use one loop for every element type

    링크
    main.tpzUse one loop for every element type플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let mut sum = 0
    4. for value in ([2, 3, 5]) {
    5. sum += value
    6. }
    7. let mut trueCount = 0
    8. for value in ([true, false, true]) {
    9. if (value) { trueCount += 1 }
    10. }
    11. let mut labels = ""
    12. for value in (["A", "B", "C"]) {
    13. labels = labels + value
    14. }
    15. test.assertEq(sum, 10)
    16. test.assertEq(trueCount, 2)
    17. test.assertEq(labels, "ABC")
    18. io.writeLine("all values checked")
    기대 출력종료 코드 0
    all values checked
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. all values checked

    Reject the removed forAllInt helper

    링크
    main.tpzReject the removed forAllInt helper플레이그라운드 열기 →
    1. import std.test { forAllInt }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 resolve. 종료 코드가 0이 아닙니다.

    Reject the removed forAllBool helper

    링크
    main.tpzReject the removed forAllBool helper플레이그라운드 열기 →
    1. import std.test { forAllBool }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 resolve. 종료 코드가 0이 아닙니다.

    Diagnostics

    링크

    Name is not exported →

    Assert inequality and text containment

    링크
    규칙topaz.stdlib.contract.test-comparisons.rule

    Assert inequality and text containment

    assertNe(actual, expected) returns unit when ordinary value equality says the operands differ. It performs no string conversion before comparison.

    The operands must have a common comparable type under the ordinary equality rules. Known noncomparable values produce TPZ5007 during checking. A comparison fault propagates with its own diagnostic.

    assertContains(text, needle) returns unit when the exact needle occurs as a contiguous substring. Matching is case sensitive and preserves Unicode spelling. An empty needle succeeds even when the text is empty.

    Arguments are evaluated once from left to right before testing the condition. Successful calls have no further side effects and print nothing.

    A false assertion raises TPZ4007 at the assertion call. It returns no value and stops the current test before the next statement. Earlier argument side effects remain visible. Test runner handling of later test cases follows the test execution contract.

    The inequality failure message uses the ordinary rendering of the shared value in this form.

    assertNe failed: both values are `{value}`

    The containment failure message includes the original text and needle in this form.

    assertContains failed: `{text}` does not contain `{needle}`

    These assertions add test failure reporting to ordinary equality and string containment. They do not define a separate comparison relation.

    이유

    Tests should disagree with a program value only when the same ordinary comparison disagrees. Silent success keeps output useful, and immediate failure prevents a false premise from affecting later assertions.

    Compare values and exact substrings

    링크
    main.tpzCompare values and exact substrings플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. test.assertNe([1, 2], [1, 3])
    4. let present: Option<int> = Some(1)
    5. let absent: Option<int> = None()
    6. test.assertNe(present, absent)
    7. test.assertContains("Topaz", "opa")
    8. test.assertContains("é", "é")
    9. test.assertContains("text", "")
    10. test.assertContains("", "")
    11. io.writeLine("comparisons passed")
    기대 출력종료 코드 0
    comparisons passed
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. comparisons passed

    Evaluate each operand once in source order

    링크
    main.tpzEvaluate each operand once in source order플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. function operand(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. let result: () = test.assertNe(operand("actual", 3), operand("expected", 4))
    8. io.writeLine("{result}")
    기대 출력종료 코드 0
    actual
    expected
    ()
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. actual
    2. expected
    3. ()

    Stop after equal operands fail assertNe

    링크
    main.tpzStop after equal operands fail assertNe플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. function operand(label: string, value: int) -> int {
    4. io.writeLine(label)
    5. return value
    6. }
    7. test.assertNe(operand("actual", 7), operand("expected", 7))
    8. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    actual
    expected
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    기대 표준 출력
    1. actual
    2. expected

    Keep substring matching case sensitive

    링크
    main.tpzKeep substring matching case sensitive플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertContains("Topaz", "topaz")
    5. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    기대 표준 출력
    1. before

    Keep composed and decomposed spellings distinct

    링크
    main.tpzKeep composed and decomposed spellings distinct플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertContains("é", "é")
    5. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    • TPZ4007 →

      stdlib-assert-contains-spelling at main.tpz:5:1

    기대 표준 출력
    1. before

    Reject values that ordinary equality cannot compare

    링크
    main.tpzReject values that ordinary equality cannot compare플레이그라운드 열기 →
    1. import std.test
    2. let value = () => 1
    3. test.assertNe(value, value)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    • TPZ5007 →

      stdlib-assert-ne-non-comparable at main.tpz:4:1

    Accept exact substrings and an empty needle

    링크
    main.tpzAccept exact substrings and an empty needle플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. test.assertContains("Topaz", "opa")
    4. test.assertContains("é", "é")
    5. test.assertContains("text", "")
    6. test.assertContains("", "")
    7. io.writeLine("text contained")
    기대 출력종료 코드 0
    text contained
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. text contained

    Diagnostics

    링크

    Assertion failed →

    Value is not comparable or keyable →

    Assert a Result constructor and return its payload

    링크
    규칙topaz.stdlib.contract.test-result-variants.rule

    Assert a Result constructor and return its payload

    assertOk accepts a Result<T, E>. It returns the T payload of Ok(value). An Err(error) raises TPZ4007.

    assertErr accepts a Result<T, E>. It returns the E payload of Err(error). An Ok(value) raises TPZ4007.

    The returned payload is unchanged. No conversion, recursive unwrapping, or equality comparison occurs. A nested Err inside an outer Ok still satisfies assertOk.

    The argument is evaluated once before its outer constructor is inspected. Success prints nothing. Failure reports the assertion call and stops the current test without running its next statement. Earlier effects remain visible.

    The failure messages identify the unexpected constructor and its ordinarily rendered payload.

    assertOk failed: got Err({error})
    assertErr failed: got Ok({value})

    These helpers express a test expectation. Match the Result in ordinary code when either constructor is part of normal control flow.

    이유

    Inspecting the outer constructor preserves the distinction between an expected failure value and a failed test. Returning the payload lets later assertions examine its contents without repeating the match.

    Extract both Result payloads and preserve a nested Err

    링크
    main.tpzExtract both Result payloads and preserve a nested Err플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let okValue: Result<int, string> = Ok(7)
    4. let errValue: Result<int, string> = Err("bad")
    5. let nested: Result<Result<int, string>, string> = Ok(Err("inner"))
    6. let value = test.assertOk(okValue)
    7. let error = test.assertErr(errValue)
    8. let inner: Result<int, string> = test.assertOk(nested)
    9. let nestedError = test.assertErr(inner)
    10. io.writeLine("{value}/{error}/{nestedError}")
    기대 출력종료 코드 0
    7/bad/inner
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 7/bad/inner

    Fail assertOk with the rendered error

    링크
    main.tpzFail assertOk with the rendered error플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let value: Result<int, string> = Err("bad")
    4. io.writeLine("before")
    5. test.assertOk(value)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    기대 표준 출력
    1. before

    Fail assertErr with the rendered value

    링크
    main.tpzFail assertErr with the rendered value플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let value: Result<int, string> = Ok(7)
    4. io.writeLine("before")
    5. test.assertErr(value)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    기대 표준 출력
    1. before

    Assert presence or absence

    링크
    규칙topaz.stdlib.contract.test-option-variants.rule

    Assert presence or absence

    assertSome accepts an Option<T> and returns the T payload of Some(value). Passing None() raises TPZ4007.

    assertNone accepts an Option<T> and returns unit for None(). Passing Some(value) raises TPZ4007.

    Presence depends on the outer constructor. Some(None()), Some(false), and Some(0) are present values. assertSome returns their payloads unchanged.

    Each argument is evaluated once. A successful assertion prints nothing and performs no additional effects. Failure reports the assertion call and stops the current test before its next statement.

    The failure messages identify the unexpected constructor. A present payload uses ordinary value rendering.

    assertSome failed: got None
    assertNone failed: got Some({value})

    Use a match to handle absence as normal program behavior. These assertions report a failed test when the chosen presence expectation is false.

    이유

    Outer constructor checks keep absence separate from a stored false, zero, or absent nested value. They give tests the same Option meaning as ordinary pattern matching.

    Distinguish nested absence from an absent outer value

    링크
    main.tpzDistinguish nested absence from an absent outer value플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let present: Option<int> = Some(7)
    4. let absent: Option<int> = None()
    5. let nested: Option<Option<int>> = Some(None())
    6. let value = test.assertSome(present)
    7. let result: () = test.assertNone(absent)
    8. test.assertNone(test.assertSome(nested))
    9. io.writeLine("{value}/{result}")
    기대 출력종료 코드 0
    7/()
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 7/()

    Fail assertSome on an absent value

    링크
    main.tpzFail assertSome on an absent value플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let value: Option<int> = None()
    4. io.writeLine("before")
    5. test.assertSome(value)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    기대 표준 출력
    1. before

    Fail assertNone with the present payload

    링크
    main.tpzFail assertNone with the present payload플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. let value: Option<int> = Some(7)
    4. io.writeLine("before")
    5. test.assertNone(value)
    6. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    기대 표준 출력
    1. before

    Compare existing golden text without updating it

    링크
    규칙topaz.stdlib.contract.test-golden-text.rule

    Compare existing golden text without updating it

    assertGolden(path, actual) accepts two strings. Read the existing file as UTF-8 and compare the entire decoded text with actual. UTF-8 validity and text preservation follow UTF-8 decoding. Equal text returns unit without printing.

    Resolve the path using the same host path and read permission rules as filesystem text reads. For an ordinary installed CLI program, a relative path starts at the process working directory. The source file directory does not replace that directory.

    Comparison preserves every scalar and line ending. CRLF and LF differ. A final newline, a byte order mark, Unicode spelling, and an empty file remain significant. Invalid UTF-8 is a read failure.

    Evaluate the path expression and then the actual expression, once each, before reading the file. Open and read the expected text once. Release any opened resource before returning or reporting a read or comparison failure.

    The assertion needs read access. It never creates a missing golden file, replaces its contents, or enables an update mode. Write a chosen expected result explicitly through the filesystem API.

    Missing files, directories that cannot be read as text, permission denial, and invalid UTF-8 raise TPZ4007 at the assertion call. File failures identify the path and whether opening or reading failed. The host detail explains the cause and may vary by platform.

    The file failure messages have these forms.

    assertGolden failed: could not open `{path}`: {detail}
    assertGolden failed: could not read `{path}`: {detail}

    Unequal text raises TPZ4007 and stops the current test before its next statement. The message has this form.

    assertGolden failed: `{path}` did not match actual output

    Earlier argument effects remain visible after failure. Reading does not promise a snapshot across concurrent host writes. Keep the expected file unchanged while the assertion reads it.

    이유

    An existing expected file gives the test an independent reference. Exact comparison preserves document bytes that matter to generated text, and explicit writes keep failed tests from replacing their own expectations.

    Match exact line endings and an empty golden file

    링크
    main.tpzMatch exact line endings and an empty golden file플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. test.assertGolden("expected.txt", "alpha\r\nβ\n")
    4. test.assertGolden("empty.txt", "")
    5. io.writeLine("golden text matched")
    기대 출력종료 코드 0
    golden text matched
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    expected.txtMatch exact line endings and an empty golden file플레이그라운드 열기 →
    1. alpha
    2. β
    empty.txtMatch exact line endings and an empty golden file플레이그라운드 열기 →
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. golden text matched

    Resolve a golden path from the working directory

    링크
    nested/main.tpzResolve a golden path from the working directory플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. test.assertGolden("expected.txt", "working directory\n")
    4. io.writeLine("working directory matched")
    기대 출력종료 코드 0
    working directory matched
    
    $ topaz run nested/main.tpz · 대상 제품 5.21.3
    expected.txtResolve a golden path from the working directory플레이그라운드 열기 →
    1. working directory
    nested/expected.txtResolve a golden path from the working directory플레이그라운드 열기 →
    1. source directory
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. working directory matched

    Fail when the final newline is missing from actual text

    링크
    main.tpzFail when the final newline is missing from actual text플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertGolden("expected.txt", "line")
    5. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    expected.txtFail when the final newline is missing from actual text플레이그라운드 열기 →
    1. line
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    • TPZ4007 →

      stdlib-assert-golden-mismatch at main.tpz:5:1

    기대 표준 출력
    1. before

    Fail instead of creating a missing golden file

    링크
    main.tpzFail instead of creating a missing golden file플레이그라운드 열기 →
    1. import std.test
    2. import std.io
    3. io.writeLine("before")
    4. test.assertGolden("missing.txt", "new text")
    5. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    • TPZ4007 →

      stdlib-assert-golden-missing at main.tpz:5:1

    기대 표준 출력
    1. before

    Fail instead of replacing invalid UTF-8 with a character

    링크
    main.tpzFail instead of replacing invalid UTF-8 with a character플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.fs
    3. import std.test
    4. import std.io
    5. let bytes = test.assertOk(Bytes.fromArray([255]))
    6. test.assertOk(fs.writeBytes("invalid.txt", bytes))
    7. io.writeLine("before")
    8. test.assertGolden("invalid.txt", "�")
    9. io.writeLine("after")
    기대 출력0이 아닌 종료 코드
    before
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 run. 종료 코드가 0이 아닙니다.

    • TPZ4007 →

      stdlib-assert-golden-invalid-utf8 at main.tpz:9:1

    기대 표준 출력
    1. before

    메뉴

    다운로드 →