testdiff

package module
v0.0.0-...-ae148f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2018 License: BSD-3-Clause Imports: 4 Imported by: 0

README

testdiff

Build Status Test Coverage Go Report Card

testdiff is a simple library specifically for testing purposes, which adds a very simple function for comparing two strings.

Installing testdiff

Install testdiff by calling:

go get -u github.com/catherinejones/testdiff

Using testdiff

Import testdiff by calling

import "github.com/catherinejones/testdiff"

testdiff's main functions are:

  1. StringIs
  2. StringIsNot
  3. StringIsFile
  4. StringIsNotFile

StringIs and StringIsNot test if a string is a specific value. StringIs returns a diff if the two passed strings are not equal:

import (
    "testing"
    "github.com/catherinejones/testdiff"
)

func TestExample(t *testing.T) {
    testdiff.StringIs(t, exampleString, exampleString)
    testdiff.StringIsNot(t, exampleString, exampleDiffString)
}

Both of the tests in TestExample should pass.

import (
    "testing"
    "github.com/catherinejones/testdiff"
)

func TestBadExample(t *testing.T) {
    testdiff.StringIs(t, exampleString, exampleDiffString)
}

This example would return an error message with the diff in the test output.

The error message showing that there are a few character differences between the two strings

StringIsFile and StringIsNotFile test if a string is equal to the contents of the passed filename. StringIsFile, like StringIs, will return a diff if the string doesn't match the contents of the file at the passed filename:

import (
    "testing"
    "github.com/catherinejones/testdiff"
)

func TestFileExample(t *testing.T) {
    // "exampleString.txt" contains the contents of exampleString.
    testdiff.StringIsFile(t, "exampleString.txt", exampleString)
    testdiff.StringIsNotFile(t, "exampleString.txt", exampleDiffString)
}

Both of the tests in TestFileExample should pass.

The low-level way of interacting with testdiff is to use the CompareStrings function. It takes two strings as input, and returns true or false depending on if they're equal or not, respectively. It also returns an error, with a listing of the lines that differ between the two strings.

import "github.com/catherinejones/testdiff"

func Example() {
    compare, err = testdiff.CompareStrings(exampleString, exampleDiffString)
    if !compare {
        fmt.Println("Error: ", err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareStrings

func CompareStrings(a, b string) (bool, error)

CompareStrings compares two strings line by line and returns true if they are equal. Returns false and an error with the lines that failed if not.

func StringIs

func StringIs(t *testing.T, a, b string)

StringIs compares two strings line by line and gives an error if they are not equal with the diff.

func StringIsFile

func StringIsFile(t *testing.T, filename, b string)

StringIsFile compares a string to the file at the passed filename line by line and gives an error if they are not equal with the diff.

func StringIsNot

func StringIsNot(t *testing.T, a, b string)

StringIsNot compares two strings line by line and gives an error if they are equal.

func StringIsNotFile

func StringIsNotFile(t *testing.T, filename, b string)

StringIsNotFile compares a string to the file at the passed filename line by line and gives an error if they are equal.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL