Created
May 24, 2019 18:42
-
-
Save rz7d/5c851e19327a665aa7fd30fdaedd70e3 to your computer and use it in GitHub Desktop.
Fluent Assertions (No Supports)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package assertgo | |
/* | |
* Copyright (C) 2019 azure | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
*/ | |
import ( | |
"encoding/json" | |
"fmt" | |
"reflect" | |
) | |
type Assertion struct { | |
actual *interface{} | |
} | |
func Shout(expected interface{}, actual interface{}) error { | |
ex, e := json.Marshal(expected) | |
if e != nil { | |
return e | |
} | |
ac, e := json.Marshal(actual) | |
if e != nil { | |
return e | |
} | |
msg := fmt.Sprintf("Test Failed. expected: %s, actual: %s", ex, ac) | |
for _, p := range []func(string) error{func(it string) error { return fmt.Errorf(it) }, func(it string) error { _, err := fmt.Printf(it); return err }} { | |
p(msg) | |
} | |
return nil | |
} | |
func AssertThat(actual interface{}) *Assertion { | |
return &Assertion{&actual} | |
} | |
func (this *Assertion) IsEqual(expected interface{}) (bool, error) { | |
r := reflect.DeepEqual(expected, *(this.actual)) | |
if !r { | |
return false, Shout(expected, *(this.actual)) | |
} | |
return r, nil | |
} | |
func (this *Assertion) IsSame(expected interface{}) (bool, error) { | |
r := reflect.DeepEqual(&expected, this.actual) | |
if !r { | |
return false, Shout(&expected, this.actual) | |
} | |
return r, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment