How do I assert that an Option contains something in zio-test? - scala

I tried
assert(anOption)(contains("x"))
But that only works for Iterables such as List or Seq.

assert(anOption)(isSome(equalTo("x")))

Could also be assert(anOption)(equalTo(Some("x"))) - just using equality. Or if you want to use contains: assert(isTrue(anOption.contains("x"))) using the contains operator on option

Related

How to sort an array filled with Timestamps?

I have a list where its elements are Timestamps in the form of
Timestamp(seconds=..., nanoseconds=...)
so I got
List myarr = [Timestamp(seconds=..., nanoseconds=...),Timestamp(seconds=..., nanoseconds=...),Timestamp(seconds=..., nanoseconds=...)]
How can I order this list? I have tried calling myarr.sort() but then I got the following error:
This expression has a type of 'void' so its value can't be used.
Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.
How can I sort the above mentioned array?
What is Timestamp? Is it perhaps firebase Timestamp?
Either way, it has to implement Comparable if it should be used without defining the sort method manually.
Otherwise you'll have to do myArr.sort((a,b) => "do your own sort")
For sort it in asc:
myarr.sort((a, b) => a.milliseconds - b.milliseconds);
For sort it desc
myarr.sort((a, b) => b.milliseconds - a.milliseconds);
Those are good answers, thank you. However, for me the following worked:
..sort()

How to check if string is valid by checking if its value is in array

joi.string().valid(['foo', 'bar']) has been deprecated.
Error: Method no longer accepts array arguments: allow
What is the new way of achieving this?
Using spread syntax works perfectly!
joi.string().valid(...['foo', 'bar'])

Filter, Option or FlatMap in spark

I have next code in Spark:
dsPhs.filter(filter(_))
.map(convert)
.coalesce(partitions)
.filter(additionalFilter.IsValid(_))
At convert function I get more complex object - MyObject, so I need to prefilter basic object. I have 3 options:
Make map return option(MyObject) and filter it at additionalFilter
Replace map with flatMap and return empty array when filtered
Use filter before map function, to filter out RawObject, before converting it to MyObject.
Now I am go with option 3. But may be 1 or 2 is more preferable?
If, for option 2, you mean have convert return an empty array, there's another option: have convert return an Option[MyObject] and use flatMap instead of map. This has the best of options 1 and 2. Without knowing more about your use case, I can't say for sure whether this is better than option 3, but here are some considerations:
Should convert contain input validation logic? If so, consider modifying it to return an Option.
If convert is used, or will be used, in other places, could they benefit from this validation?
As a side note, this might be a good time to consider what convert currently does when passed an invalid argument.
Can you easily change convert and its signature? If not, consider using a filter.

how to test if a string DON'T match using protractor

I'm migrating my karma-ng-scenario tests suite to protractor. I would like to do something like
// karma-ng-scenario
expect(element('legend').text()).not().toBe("LOGIN_CONNECT");
in the protractor way. But it seems there isn't a not() function.
I'm using angular-translate to bind the LONGIN_CONNECT string into multiple languages and I want to test if the string is translated.
More globally, is there a a way test if something is different ? ... don't have a class, don't exists on the page, is not selected, ...
It is definitely worth looking at the API docs. I have these open pretty much all the time.
There are lots of Web Driver functions you can use like isEnabled(), isDisplayed(), isSelected() etc. Protractor uses Jasmine syntax so you can use '.toBe(false)' to assert things are false.
To check for classes, you can do something like:
expect(myElement.getAttribute('class')).toContain('my-class-name');
To compare strings and assert that they do NOT match you could use .not. Jasmine docs
say:
Every matcher's criteria can be inverted by prepending .not:
expect(x).not.toEqual(y); compares objects or primitives x and y and
passes if they are not equivalent
You can use something like:
expect(model.getText()).not.toContain('abcdef');
There is a .not property nowadays.
I'm using the following to check for NOT matching:
expect(element('legend').text() === "LOGIN_CONNECT").toBe(false);

How to create sorted map in scala?

How to create sorted map in scala (mutable/immutable)?
Use a TreeMap.
There's always java.util.TreeMap which, although not as nice as Scala's maps, is probably exactly the behavior for which you're looking.