Skip to content

Latest commit

 

History

History
1108 lines (876 loc) · 23.4 KB

File metadata and controls

1108 lines (876 loc) · 23.4 KB

SharpAssert Demo Output

This document showcases all supported features of SharpAssert with detailed diagnostic output.


1. BASIC ASSERTIONS

Fundamental assertion failures showing expression text and values

Simple Failure

Description: Basic assertion failure with expression text

Code:

Assert(false);

Output:

Assertion failed: false  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/01_BasicAssertionsDemos.cs:12
  False

Expression Text

Description: Shows operands and result for comparison expressions

Code:

Assert(1 == 2);

Output:

Assertion failed: 1 == 2  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/01_BasicAssertionsDemos.cs:20
  False

Custom Message

Description: Assertion with custom failure message

Code:

Assert(false, "This is a custom failure message");

Output:

This is a custom failure message
Assertion failed: false  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/01_BasicAssertionsDemos.cs:28
  False

Complex Expression

Description: Multi-variable expression with operators

Code:

var x = 10;
var y = 5;
var z = 3;
Assert(x + y * z > 100);

Output:

Assertion failed: x + y * z > 100  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/01_BasicAssertionsDemos.cs:39
  x + y * z > 100
    Left:  25
    Right: 100

2. BINARY COMPARISONS

Equality, inequality, and relational operator comparisons

Equality Operators

Description: == and != showing left and right values

Code:

var actual = 42;
var expected = 100;
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/02_BinaryComparisonDemos.cs:14
  actual == expected
    Left:  42
    Right: 100

Relational Operators

Description: <, <=, >, >= with numbers

Code:

var value = 5;
var threshold = 10;
Assert(value > threshold);

Output:

Assertion failed: value > threshold  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/02_BinaryComparisonDemos.cs:24
  value > threshold
    Left:  5
    Right: 10

Null Comparisons

Description: null vs non-null showing both sides

Code:

string? nullValue = null;
var nonNullValue = "text";
Assert(nullValue == nonNullValue);

Output:

Assertion failed: nullValue == nonNullValue  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/02_BinaryComparisonDemos.cs:34
  nullValue == nonNullValue
    Left:  null
    Right: "text"
    Diff: [-null][+"text"]

Single Evaluation

Description: Method calls happen only once

Code:

callCount = 0;
Assert(GetValue() == 100);

Output:

Assertion failed: GetValue() == 100  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/02_BinaryComparisonDemos.cs:50
  GetValue() == 100
    Left:  42
    Right: 100

Type Mismatch

Description: Comparing different types

Code:

object intValue = 42;
object stringValue = "42";
Assert(intValue.Equals(stringValue));

Output:

Assertion failed: intValue.Equals(stringValue)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/02_BinaryComparisonDemos.cs:60
  intValue.Equals(stringValue)
    Argument[0]: "42"
  Result: False

3. LOGICAL OPERATORS

AND, OR, NOT operators with short-circuit evaluation

AND Failure

Description: Shows which operand failed

Code:

var left = true;
var right = false;
Assert(left && right);

Output:

Assertion failed: left && right  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/03_LogicalOperatorDemos.cs:14
  left && right
  Left: True
  Right: False
  &&: Right operand was false

Short-Circuit AND

Description: Right side not evaluated when left is false

Code:

var condition = false;
Assert(condition && ThrowsException());

Output:

Assertion failed: condition && ThrowsException()  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/03_LogicalOperatorDemos.cs:23
  condition && ThrowsException()
  Left: False
  &&: Left operand was false

OR Failure

Description: Both operands evaluated and shown

Code:

var left = false;
var right = false;
Assert(left || right);

Output:

Assertion failed: left || right  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/03_LogicalOperatorDemos.cs:38
  left || right
  Left: False
  Right: False
  ||: Both operands were false

NOT Operator

Description: Shows the actual value being negated

Code:

var value = true;
Assert(!value);

Output:

Assertion failed: !value  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/03_LogicalOperatorDemos.cs:47
  !value
  Operand: True
  !: Operand was True

Nested Logical

Description: Complex expressions with multiple operators

Code:

var a = true;
var b = false;
var c = true;
var d = false;
Assert((a && b) || (c && d));

Output:

Assertion failed: (a && b) || (c && d)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/03_LogicalOperatorDemos.cs:59
  (a && b) || (c && d)
  Left: (a && b)
  Left: True
  Right: False
  &&: Right operand was false
  Right: (c && d)
  Left: True
  Right: False
  &&: Right operand was false
  ||: Both operands were false

4. STRING COMPARISONS

Single-line and multiline string diffs with character-level highlighting

Single-Line Diff

Description: Inline character diff for single-line strings

Code:

var actual = "hello world";
var expected = "hallo world";
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/04_StringComparisonDemos.cs:14
  actual == expected
    Left:  "hello world"
    Right: "hallo world"
    Diff: h[-e][+a]llo world

Multiline Diff

Description: Line-by-line comparison for multiline strings

Code:

var actual = """
    Line 1: Introduction
    Line 2: Body content
    Line 3: Conclusion
    """;
var expected = """
    Line 1: Introduction
    Line 2: Different content
    Line 3: Conclusion
    """;
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/04_StringComparisonDemos.cs:32
  actual == expected
    Left:
      Line 1: Introduction
      Line 2: Body content
      Line 3: Conclusion
    Right:
      Line 1: Introduction
      Line 2: Different content
      Line 3: Conclusion
    Diff:
        Line 1: Introduction
      - Line 2: Body content
      + Line 2: Different content
        Line 3: Conclusion

Null vs String

Description: null compared to non-null string

Code:

string? nullString = null;
var nonNullString = "text";
Assert(nullString == nonNullString);

Output:

Assertion failed: nullString == nonNullString  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/04_StringComparisonDemos.cs:42
  nullString == nonNullString
    Left:  null
    Right: "text"
    Diff: [-null][+"text"]

Empty vs Non-Empty

Description: Empty string vs text

Code:

var empty = "";
var nonEmpty = "text";
Assert(empty == nonEmpty);

Output:

Assertion failed: empty == nonEmpty  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/04_StringComparisonDemos.cs:52
  empty == nonEmpty
    Left:  ""
    Right: "text"
    Diff: [+text]

Long Strings

Description: Long text with diff and truncation

Code:

var actual = "The quick brown fox jumps over the lazy dog. This is a very long string that demonstrates how SharpAssert handles lengthy text comparisons with proper formatting and truncation when necessary.";
var expected = "The quick brown fox jumps over the lazy cat. This is a very long string that demonstrates how SharpAssert handles lengthy text comparisons with proper formatting and truncation when necessary.";
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/04_StringComparisonDemos.cs:62
  actual == expected
    Left:  "The quick brown fox jumps over the lazy dog. This is a very long string that demonstrates how SharpAssert handles lengthy text comparisons with proper formatting and truncation when necessary."
    Right: "The quick brown fox jumps over the lazy cat. This is a very long string that demonstrates how SharpAssert handles lengthy text comparisons with proper formatting and truncation when necessary."
    Diff: The quick brown fox jumps over the lazy [-dog][+cat]. This is a very long string that demonstrates how SharpAssert handles lengthy text comparisons with proper formatting and truncation when necessary.

5. COLLECTION COMPARISONS

Array and list comparisons showing differences, missing, and extra elements

First Mismatch

Description: Shows first element that differs

Code:

var actual = new[] { 1, 2, 3 };
var expected = new[] { 1, 2, 4 };
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/05_CollectionComparisonDemos.cs:14
  actual == expected
    Left:  [1, 2, 3]
    Right: [1, 2, 4]
    First difference at index 2: expected 3, got 4

Missing Elements

Description: Shows elements present in expected but not actual

Code:

var actual = new[] { 1, 2 };
var expected = new[] { 1, 2, 3 };
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/05_CollectionComparisonDemos.cs:24
  actual == expected
    Left:  [1, 2]
    Right: [1, 2, 3]
    Missing elements: [3]

Extra Elements

Description: Shows elements in actual but not expected

Code:

var actual = new[] { 1, 2, 3 };
var expected = new[] { 1, 2 };
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/05_CollectionComparisonDemos.cs:34
  actual == expected
    Left:  [1, 2, 3]
    Right: [1, 2]
    Extra elements: [3]

Empty Collection

Description: Empty vs non-empty collection

Code:

var actual = Array.Empty<int>();
var expected = new[] { 1 };
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/05_CollectionComparisonDemos.cs:44
  actual == expected
    Left:  []
    Right: [1]
    Missing elements: [1]

Different Lengths

Description: Collections with different sizes

Code:

var actual = new[] { 1, 2, 3, 4, 5 };
var expected = new[] { 1, 2 };
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/05_CollectionComparisonDemos.cs:54
  actual == expected
    Left:  [1, 2, 3, 4, 5]
    Right: [1, 2]
    Extra elements: [3, 4, 5]

Large Collections

Description: Preview truncation for large collections

Code:

var actual = Enumerable.Range(1, 100).ToArray();
var expected = Enumerable.Range(1, 100).Select(x => x == 50 ? 999 : x).ToArray();
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/05_CollectionComparisonDemos.cs:64
  actual == expected
    Left:  [1, 2, 3, 4, 5, 6, 7, 8, 9, "..."]
    Right: [1, 2, 3, 4, 5, 6, 7, 8, 9, "..."]
    First difference at index 49: expected 50, got 999

6. OBJECT COMPARISONS

Deep object comparison showing property-level differences

Property Difference

Description: Simple object with different property values

Code:

var actual = new Person("Alice", 30, "New York");
var expected = new Person("Alice", 25, "New York");
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/06_ObjectComparisonDemos.cs:18
  actual == expected
    Property differences:
      Age: expected "30", got "25"

Nested Objects

Description: Deep property paths like Address.City

Code:

var actual = new Customer(
    "Bob",
    new Address("123 Main St", "Boston", "02101"));
var expected = new Customer(
    "Bob",
    new Address("123 Main St", "New York", "10001"));
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/06_ObjectComparisonDemos.cs:32
  actual == expected
    Property differences:
      Address.City: expected "Boston", got "New York"
      Address.ZipCode: expected "02101", got "10001"

Null Object

Description: null vs object instance

Code:

Person? nullPerson = null;
var nonNullPerson = new Person("Charlie", 35, "Chicago");
Assert(nullPerson == nonNullPerson);

Output:

Assertion failed: nullPerson == nonNullPerson  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/06_ObjectComparisonDemos.cs:42
  nullPerson == nonNullPerson
    Left:  null
    Right: Person { Name = Charlie, Age = 35, City = Chicago }

Record Comparison

Description: Record type value equality

Code:

var actual = new Person("David", 40, "Denver");
var expected = new Person("David", 40, "Detroit");
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/06_ObjectComparisonDemos.cs:52
  actual == expected
    Property differences:
      City: expected "Denver", got "Detroit"

Multiple Differences

Description: Multiple properties differ

Code:

var actual = new Person("Eve", 28, "Seattle");
var expected = new Person("Eva", 30, "Portland");
Assert(actual == expected);

Output:

Assertion failed: actual == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/06_ObjectComparisonDemos.cs:62
  actual == expected
    Property differences:
      Name: expected "Eve", got "Eva"
      Age: expected "28", got "30"
      City: expected "Seattle", got "Portland"

7. LINQ OPERATIONS

Contains, Any, All with predicates showing matching/failing items

Contains Failure

Description: Shows collection contents when item not found

Code:

var items = new[] { 1, 2, 3, 4, 5 };
var missingItem = 10;
Assert(items.Contains(missingItem));

Output:

Assertion failed: items.Contains(missingItem)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/07_LinqOperationDemos.cs:14
  items.Contains(missingItem)
    Contains failed: searched for 10 in [1, 2, 3, 4, 5]
    Count: 5

Any with Predicate

Description: Shows items matching predicate

Code:

var items = new[] { 1, 2, 3, 4, 5 };
Assert(items.Any(x => x > 10));

Output:

Assertion failed: items.Any(x => x > 10)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/07_LinqOperationDemos.cs:23
  items.Any(x => x > 10)
    Any failed: no items matched x => (x > 10) in [1, 2, 3, 4, 5]

All with Predicate

Description: Shows items failing predicate

Code:

var items = new[] { 1, 2, 3, 4, 5 };
Assert(items.All(x => x > 3));

Output:

Assertion failed: items.All(x => x > 3)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/07_LinqOperationDemos.cs:32
  items.All(x => x > 3)
    All failed: items [1, 2, 3] did not match x => (x > 3)

Empty Collection

Description: LINQ on empty collection

Code:

var items = Array.Empty<int>();
Assert(items.Any());

Output:

Assertion failed: items.Any()  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/07_LinqOperationDemos.cs:41
  items.Any()
    Any failed: collection is empty

8. SEQUENCE EQUAL

SequenceEqual with unified diff display

Unified Diff

Description: Side-by-side sequence comparison

Code:

var actual = new[] { 1, 2, 3, 4, 5 };
var expected = new[] { 1, 2, 9, 4, 5 };
Assert(actual.SequenceEqual(expected));

Output:

Assertion failed: actual.SequenceEqual(expected)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/08_SequenceEqualDemos.cs:14
  actual.SequenceEqual(expected)
    SequenceEqual failed: sequences differ
    Unified diff:
        [0] = 1
        [1] = 2
      - [2] = 3
      + [2] = 9

Different Lengths

Description: Sequences of different sizes

Code:

var actual = new[] { 1, 2, 3 };
var expected = new[] { 1, 2, 3, 4, 5 };
Assert(actual.SequenceEqual(expected));

Output:

Assertion failed: actual.SequenceEqual(expected)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/08_SequenceEqualDemos.cs:24
  actual.SequenceEqual(expected)
    SequenceEqual failed: length mismatch
    Expected length: 5
    Actual length:   3
    First:  [1, 2, 3]
    Second: [1, 2, 3, 4, 5]

Element-by-Element

Description: Detailed element comparison

Code:

var actual = new[] { "apple", "banana", "cherry" };
var expected = new[] { "apple", "orange", "cherry" };
Assert(actual.SequenceEqual(expected));

Output:

Assertion failed: actual.SequenceEqual(expected)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/08_SequenceEqualDemos.cs:34
  actual.SequenceEqual(expected)
    SequenceEqual failed: sequences differ
    Unified diff:
        [0] = "apple"
      - [1] = "banana"
      + [1] = "orange"

Large Sequences

Description: Truncation for large sequences

Code:

var actual = Enumerable.Range(1, 50).ToArray();
var expected = Enumerable.Range(1, 50).Select(x => x == 25 ? 999 : x).ToArray();
Assert(actual.SequenceEqual(expected));

Output:

Assertion failed: actual.SequenceEqual(expected)  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/08_SequenceEqualDemos.cs:44
  actual.SequenceEqual(expected)
    SequenceEqual failed: sequences differ
    Unified diff:
        [21] = 22
        [22] = 23
        [23] = 24
      - [24] = 25
      + [24] = 999

9. ASYNC OPERATIONS

Async/await assertions showing awaited values

Basic Await

Description: Simple async condition

Code:

Assert(await GetBoolAsync());

Output:

Assertion failed: await GetBoolAsync()  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/09_AsyncDemos.cs:36
  False

Async Binary

Description: Both sides awaited with values shown

Code:

Assert(await GetLeftValueAsync() == await GetRightValueAsync());

Output:

Assertion failed: await GetLeftValueAsync() == await GetRightValueAsync()  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/09_AsyncDemos.cs:44
  await GetLeftValueAsync() == await GetRightValueAsync()
    Left:  42
    Right: 100

Mixed Async/Sync

Description: Await on one side, constant on other

Code:

Assert(await GetLeftValueAsync() == 100);

Output:

Assertion failed: await GetLeftValueAsync() == 100  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/09_AsyncDemos.cs:52
  await GetLeftValueAsync() == 100
    Left:  42
    Right: 100

Async String Diff

Description: Awaited string with diff

Code:

Assert(await GetStringAsync() == "expected value");

Output:

Assertion failed: await GetStringAsync() == "expected value"  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/09_AsyncDemos.cs:60
  await GetStringAsync() == "expected value"
    Left:  "actual value"
    Right: "expected value"
    Diff: [-a][+expe]ct[-ual][+ed] value

10. DYNAMIC TYPES

Dynamic type assertions with DLR evaluation

Dynamic Binary

Description: Dynamic comparison showing values

Code:

dynamic value = 42;
Assert(value == 100);

Output:

Assertion failed: value == 100  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/10_DynamicDemos.cs:13
  value == 100
    Left:  42
    Right: 100

Dynamic Method Call

Description: Method call on dynamic object

Code:

dynamic obj = new DynamicObject();
Assert(obj.GetValue() > 100);

Output:

Assertion failed: obj.GetValue() > 100  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/10_DynamicDemos.cs:22
  obj.GetValue() > 100
    Left:  42
    Right: 100

Dynamic Operators

Description: Dynamic operator semantics

Code:

dynamic left = 10;
dynamic right = 20;
Assert(left > right);

Output:

Assertion failed: left > right  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/10_DynamicDemos.cs:32
  left > right
    Left:  10
    Right: 20

11. NULLABLE TYPES

Nullable value types and reference types showing HasValue and Value

Nullable Int (null)

Description: int? null showing HasValue: false

Code:

int? value = null;
Assert(value == 42);

Output:

Assertion failed: value == 42  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:13
  value == 42
    Left:  null
    Right: 42

Nullable Int (value)

Description: int? with value showing HasValue: true, Value

Code:

int? value = 42;
Assert(value == 100);

Output:

Assertion failed: value == 100  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:22
  value == 100
    Left:  42
    Right: 100

Nullable Bool

Description: bool? comparison

Code:

bool? value = false;
Assert(value == true);

Output:

Assertion failed: value == true  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:31
  value == true
    Left:  False
    Right: True

Nullable DateTime

Description: DateTime? null comparison

Code:

DateTime? value = null;
var expected = DateTime.Now;
Assert(value == expected);

Output:

Assertion failed: value == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:41
  value == expected
    Left:  null
    Right: 12/9/2025

Nullable Reference Types

Description: string?, object? comparisons

Code:

string? value = null;
string expected = "text";
Assert(value == expected);

Output:

Assertion failed: value == expected  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:51
  value == expected
    Left:  null
    Right: "text"
    Diff: [-null][+"text"]

Null Comparison Edge Cases

Description: nullable == null

Code:

int? value = 42;
Assert(value == null);

Output:

Assertion failed: value == null  at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:60
  value == null
    Left:  42
    Right: null

Summary

Total categories demonstrated: 11 Total demo cases: 51

All assertions intentionally failed to showcase SharpAssert's rich diagnostic output.

SharpAssert provides pytest-style assertions with detailed failure messages, helping you understand exactly why your tests fail.