Golang mock for all arguments except - go

mock.on("FunctionName", "someStringArgument").Return(...)
Suppose if someStringArgument is "hello" then I want to return "1". But if someStringArgument is any other string I want to return "2".
How is this achieve able with GoMock?

What you want to do is write a custom function which will return your desired output.
Here is a simple example of what I do.
Define a custom response function
func FunctionNameResponse(arg String) string{
if arg == "hellp" {
// I used quotes because you mentioned "1" and not 1
return "1"
}
return "2"
}
Call custom function anywhere needed.
mock.on("FunctionName", mock.Anything).Return(FunctionNameResponse("someStringArgument"))

Related

Cypress if else conditional test

I am trying to include the below condition into my test but I can't seem to get it to work and receive the below error, any ideas why?
Essentially, I want to test that a input is / is not empty:
cy.get(`[class^='input-module_field']`).eq(0).then(($input) => {
if ($input.should('have.value', '')) {
cy.get(`[class^='input-module_field']`).eq(0).should('be.visible').type(foo)
cy.get(`[class^='input-module_field']`).eq(1).should('be.visible').type(bar)
cy.get(`[class^='input-module_field']`).eq(2).should('be.visible').type(foo-bar)
cy.get(`[class^='input-module_field']`).eq(3).should('be.visible').type(foo-bar-foo)
} else {
cy.get(`[class^='input-module_field']`).eq(1).should('be.visible').type(bar)
cy.get(`[class^='input-module_field']`).eq(2).should('be.visible').type(foo-bar)
cy.get(`[class^='input-module_field']`).eq(3).should('be.visible').type(foo-bar-foo)
}
})
The error i get:
$input.should is not a function
When yielded from a .then(), the $input variable is just a JQuery element, and can't use Cypress commands. In this case, even using a Cypress command, such as .should() wouldn't work, because that does not yield a boolean value for the if/else.
Instead, we'll want to use JQuery's .val() function.
...
if ($input.val()) { // if $input.val() returns an empty string, this evaluates to false
// code to run if the $input element has a value
} else {
// code to run if the $input element does not have a value.
}
...
Note: I reversed the order of what you had, with the $input.val() === '' being the else, instead of the if.
The check can be on the value itself
cy.get(`[class^='input-module_field']`).eq(0)
.then(($input) => {
const field0Val = $input.val() || 'foo' // if empty use "foo"
cy.get(`[class^='input-module_field']`).eq(0).type(field0Val)
cy.get(`[class^='input-module_field']`).eq(1).type('bar')
cy.get(`[class^='input-module_field']`).eq(2).type('foo-bar')
cy.get(`[class^='input-module_field']`).eq(3).type('foo-bar-foo')
})

swift 4.2 - how can I check with guard if a var has a valid enum value

I need to check if a var, f.e. passed by a func, is a valid enum value. Not per se passed but just as an example here.
enum CollectionDict : String { // Mapping to String Model = "Model" or should I ...
case Model
case Type
case Element
case ....
}
....
guard InColectionDict != CollectionDict else { return false }
....
Obviously my sample guards line is wrong. What should I use or do to get the guard right or at least just compare/validate the InColectionDict variable with the enum CollectionDict in a one-liner?
I did hope to get away with..
func makeItem ( _ item: String , with key : String , inCollection : CollectionDict ) -> Bool {
guard let res = CollectionDict.inCollection else { return false }
But it give me an error.
Of course thank you in advance.
Swift is strongly typed. If your function has an non-optional Enum parameter, then at run-time it's guaranteed to be a valid enum value.

Reflect to test if a value is a string

I'm trying to test if a value coming from the gjson library is a string in the quickest and simplest way possible. I don't want to use a switch type assertion.
if reflect.TypeOf(gjson.Get(input, "name").Value()) != "string" {
return "Not a string!"
}
What's wrong with my code?
gjson.Get returns a Result, so you can simply check its Type field:
if gjson.Get(input, "name").Type != gjson.String {
return "Not a string!"
}

Initializing structures dynamically

I have a couple of structures, like:
type SomeObject struct {
sample int
}
I want to fill the sample variable based on what I get in the request body. To do this, I want to create a function, pass request body as a string to it, create an empty structure inside, fill the structure with data, return it, and replace chosen structure with this.
How do I do this? What do I return from the function? Is there a way to do this?
If you're dealing with multiple types then you should make your method return an interface{}. For all of the applicable types, create a convenience method like;
func NewSomeObject(reqBody string) *SomeObject {
return &SomeObject{sample:reqBody}
}
Which takes a string and returns a new instance of the type with that field set to whatever was passed in. Your question is missing information about how you determine which type should be instantiated but assuming you have a few, you'll likely need an if/else or a switch in the method which receives the request body so to give a very vague example it would be something like;
func ProcessRequest(reqBody string) interface{} {
if someCondition {
return NewSomeObject(reqBody)
} else if otherCondition {
return NewSomeOtherObject(reqBody)
} // potentially several other statements like this
return nil // catch all, if no conditions match
}
How about
func foo (s *SomeObject) {
s.sample = 123
}
or
func (s *SomeObject) foo() {
s.sample = 123
}

Elasticsearch: Return empty value from script field

I'm using Elasticsearch, and I have a script field written in Groovy. It needs to replace the value of myField for some values, but I want to let most of the values pass through unchanged. The script below works except for documents that don't have the field at all. How can I make the script field value empty?
if (some criteria) {
return ...; // modified value
}
// This returns 0 if myField doesn't exist
return doc['myField'].value
I can check the empty condition, but I haven't found a way to return an empty value:
if (doc['myField'].empty)
return ???
It's perfectly ok to return null. I think the problem is more in the sequencing of your different test conditions. I've tried to reproduce your issue and I'm able to return null with the following script:
"script_fields": {
"my_script_field": {
"script": "if (doc.myField.empty) { return null } else if (some criteria) { return modifiedValue } else { return doc.myField.value }"
}
}
First check for fields with empty values and return null.
Then, you're guaranteed to have non-empty and existing fields. So you can check for your criteria and return whatever modified field you want.
Finally, you can return the unmodified values for non-empty fields which do not meet your specified criteria.
The script looks like this:
if (doc.myField.empty) {
return null
} else if (some criteria) {
return modifiedValue
} else {
return doc.myField.value
}

Resources