E81012: assertion is reachable from non-test program

Category: E81xxx-E83xxx Test configuration, execution and coverage · Reported in phase: PRE_IR_CHECKS · Classification: PRODUCTION_ASSERTION · All error codes

What went wrong

An assertion is reachable from a non-test program entry point. Assertions are for TESTING, not production code. Use 'require' instead.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 305 (What is the difference between require, assert, and thr...)
  • ek9 -q 809 (How does EK9 enforce test quality at compile time?)

[ASSERT vs REQUIRE]

  • assert → Test validation (only in @Test programs)
  • require → Production precondition (throws if condition fails)

How to fix it

  1. If the program is a test, mark it @Test: a program in dev/ without @Test

is an ordinary program, so every assertion it reaches is E81012

  1. Or change 'assert' to 'require' for production validation
  2. Or move assertion into a @Test program
  3. Or remove if check isn't needed in production

Rationale

Assertions in production indicate confused intent. EK9 enforces separation: 'require' for production, 'assert' for tests.

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E81012: assertion is reachable from non-test program

bad/dev/pricingTests.ek9

#!ek9
defines module error.e81012.bad.tests

  references
    error.e81012.bad::discounted

  defines function

    checkDiscount()
      ->
        percentOff as Integer
        expected as Integer
      assert discounted(200, percentOff) == expected

  defines program

    @Test
    TenPercentOff()
      checkDiscount(10, 180)

    HalfPrice()
      checkDiscount(50, 100)

bad/pricing.ek9

#!ek9
defines module error.e81012.bad

  defines function

    discounted() as pure
      ->
        price as Integer
        percentOff as Integer
      <- reduced as Integer: price - price * percentOff / 100

Fixed: Mark the program as the test it is

good/pricing.ek9

#!ek9
defines module error.e81012.good

  defines function

    discounted() as pure
      ->
        price as Integer
        percentOff as Integer
      <- reduced as Integer: price - price * percentOff / 100

good/dev/pricingTests.ek9

#!ek9
defines module error.e81012.good.tests

  references
    error.e81012.good::discounted

  defines function

    checkDiscount()
      ->
        percentOff as Integer
        expected as Integer
      assert discounted(200, percentOff) == expected

  defines program

    @Test
    TenPercentOff()
      checkDiscount(10, 180)

    @Test
    HalfPrice()
      checkDiscount(50, 100)

Found a problem?

If EK9 reported E81012 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.