Howto loop OrderedAssertions in FakeItEasy 2 - fakeiteasy

As I understand, ordered assertions in FakeItEasy 2 are done like this (from the docs):
// Assert
A.CallTo(() => unitOfWorkFactory.BeginWork()).MustHaveHappened()
.Then(A.CallTo(() => usefulCollaborator.JustDoIt()).MustHaveHappened())
.Then(A.CallTo(() => unitOfWork.Dispose()).MustHaveHappened());
Now, suppose I have a collection and for each item in this collection I want to assert that a call was made to a faked object. What is the best approach to assert the calls were made in the correct order?
I came up with this, but don't really like it:
IOrderableCallAssertion ioca = null;
foreach (var item in items.OrderBy(i => i.Id)
{
var itemUnderTest = item;
if (ioca == null)
{
ioca = A.CallTo(() => fakeObject.Handle(itemUnderTest, otherArgument)).MustHaveHappened(Repeated.Exactly.Once);
}
else
{
ioca = ioca.Then(A.CallTo(() => fakeObject.Handle(itemUnderTest, otherArgument)).MustHaveHappened(Repeated.Exactly.Once));
}
}

That looks about right to me. Of course, you could inline itemUnderTest and pull MustHaveHappened outside of the two if branches.
And you could always hide this in a convenience method.
An alternative: use Invokes to capture the fakes as the calls come in and later compare them against a list.

Related

Use method inside LINQ GroupBy

I'm trying to manipulate properties in a GroupBy clause to be used in a dictionary:
var lifeStages = await _dbContext.Customers
.GroupBy(x => GetLifeStage(x.DoB))
.Select(x => new { LifeStage = x.Key, Count = x.Count() })
.ToDictionaryAsync(x => x.LifeStage, x => x.Count);
I'm expecting results like
adolescent: 10,
adult: 15,
senior: 12 etc
But getting error:
Either rewrite the query in a form that can be translated,
or switch to client evaluation explicitly
by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
Offcourse I can't combine ToDictionary() with any of the mentioned calls, and splitting up the query did not resolve the issues or taught my anything)
I've tried with making GetLifeStage() static and async, no difference there as well. The method gets called, performs what it needs to do, and still GroupBy can't be translated
If I leave out the Select() part and work with the Key of the GroupBy, same error:
"...could not be translated."
I saw an error too that said I couldn't combine a GroupBy() with a ToDictionary() during try-outs, but doesn't seem to pop up atm.
As I'm running out of ideas, all suggestions are welcome!
update:
private LifeStage GetLifeStage(DateTimeOffset doB)
{
var ageInMonths = Math.Abs(12 * (doB.Year - DateTimeOffset.UtcNow.Year) + doB.Month - DateTimeOffset.UtcNow.Month);
switch (ageInMonths)
{
case < 216:
return LifeStage.Adolescent;
case < 780:
return LifeStage.Adult;
case >= 780:
return LifeStage.Senior;
}
}
The problem is the usage of the custom GetLifeStage method inside the GroupBy expression. Custom methods cannot be translated to SQL because the query translator code has no way to know what is inside that method. And it cannot be called because there are no objects at all during the translation process.
In order to make it translatable, you have to replace the custom method call with its body, converted to translatable expression - basically something which can be used as expression bodied method. You can't use variables and switch, but you can use conditional operators. Instead of variable, you could use intermediate projection (Select).
Here is the equivalent translatable query:
var lifeStages = await _dbContext.Customers
.Select(c => new { Customer = c, AgeInMonths = Math.Abs(12 * (c.DoB.Year - DateTimeOffset.UtcNow.Year) + c.DoB.Month - DateTimeOffset.UtcNow.Month) })
.GroupBy(x => x.AgeInMonths < 216 ? LifeStage.Adolescent : x.AgeInMonths < 780 ? LifeStage.Adult : LifeStage.Senior)
// the rest is the same as original
.Select(x => new { LifeStage = x.Key, Count = x.Count() })
.ToDictionaryAsync(x => x.LifeStage, x => x.Count);

Concatenating Future Lists in flutter

I am reading from 3 separate .json files via futures and want to concatenate them into one list. However, I dont't know how to, since .add and + seem to not be defined for future Lists. I also struggle to use then for further concatenation.
I want to return a Future with values from all 3 json files.
Code:
Future<List<Furniture>> getAllHousewares() {
if (_allHousewares != null) {
return _allHousewares;
}
_allHousewares = rootBundle.loadString("res/raw/housewares.json").then((json) =>
(jsonDecode(json) as List).map((houseware) => Furniture.fromJson(houseware)).toList());
_allWallmounteds = rootBundle.loadString("res/raw/Wall-mounted.json").then((json) =>
(jsonDecode(json) as List).map((wallmounted) => Furniture.fromJson(wallmounted)).toList());
_allMiscellaneouss = rootBundle.loadString("res/raw/miscellaneous.json").then((json) =>
(jsonDecode(json) as List).map((miscellaneous) => Furniture.fromJson(miscellaneous)).toList());
return _allHousewares;
}
For this case, the best way to handle it is to use the wait static method of the Future class. The method:
Waits for multiple futures to complete and collects their results.
Since all of your loaded JSON strings contain the same datatype, you can handle them all the same way with loops which leads to cleaner code. This code uses await, which is just syntactic sugar for .then.
After the data for each json is processed, it's concatenated with expand, though it could have been easily done with the + operator as well.
Example:
Future<List<Furniture>> getAllHousewares() async {
if (_allHousewares != null) {
return _allHousewares;
}
Future _allHousewares = rootBundle.loadString("res/raw/housewares.json");
Future _allWallmounteds = rootBundle.loadString("res/raw/Wall-mounted.json");
Future _allMiscellaneouss = rootBundle.loadString("res/raw/miscellaneous.json");
List combined = await Future.wait([_allHousewares, _allWallmounteds, _allMiscellaneouss ]);
List<List<Furniture>> tempFurniture= List();
for(String json in combined) {
tempFurniture.add((jsonDecode(json) as List).map((thingInList) => Furniture.fromJson(thingInList)).toList());
}
List<Furniture> furniture = tempFurniture.expand((item) => item).toList();
return furniture;
}

How do you update the CanExecute value after the ReactiveCommand has been declared

I am using ReactiveUI with AvaloniaUI and have a ViewModel with several ReactiveCommands namely Scan, Load, and Run.
Scan is invoked when an Observable<string> is updated (when I receive a barcode from a scanner).
Load is triggered from within the Scan command.
Run is triggered from a button on the UI.
Simplified code below:
var canRun = Events.ToObservableChangeSet().AutoRefresh().ToCollection().Select(x => x.Any());
Run = ReactiveCommand.CreateFromTask<bool>(EventSuite.RunAsync, canRun);
var canLoad = Run.IsExecuting.Select(x => x == false);
var Load = ReactiveCommand.CreateFromTask<string, Unit>(async (barcode) =>
{
//await - go off and load Events.
}, canLoad);
var canReceiveScan = Load.IsExecuting.Select(x => x == false)
.Merge(Run.IsExecuting.Select(x => x == false));
var Scan = ReactiveCommand.CreateFromTask<string, Unit>(async (barcode) =>
{
//do some validation stuff
await Load.Execute(barcode)
}, canReceiveScan);
Barcode
.SubscribeOn(RxApp.TaskpoolScheduler)
.ObserveOn(RxApp.MainThreadScheduler)
.InvokeCommand(Scan);
Each command can only be executed if no other command is running (including itself). But I can't reference the commands' IsExecuting property before is it declared. So I have been trying to merge the "CanExecute" observable variables like so:
canRun = canRun
.Merge(Run.IsExecuting.Select(x => x == false))
.Merge(Load.IsExecuting.Select(x => x == false))
.Merge(Scan.IsExecuting.Select(x => x == false))
.ObserveOn(RxApp.MainThreadScheduler);
// same for canLoad and canScan
The issue I'm having is that the ReactiveCommand will continue to execute when another command is executing.
Is there a better/correct way to implement this?
But I can't reference the commands' IsExecuting property before is it declared.
One option is to use a Subject<T>, pass it as the canExecute: parameter to the command, and later emit new values using OnNext on the Subject<T>.
Another option is to use WhenAnyObservable:
this.WhenAnyObservable(x => x.Run.IsExecuting)
// Here we get IObservable<bool>,
// representing the current execution
// state of the command.
.Select(executing => !executing)
Then, you can apply the Merge operator to the observables generated by WhenAnyObservable. To skip initial null values, if any, use either the Where operator or .Skip(1).
To give an example of the Subject<T> option described in the answer by Artyom, here is something inspired by Kent Boogaart's book p. 82:
var canRun = new BehaviorSubject<bool>(true);
Run = ReactiveCommand.Create...(..., canExecute: canRun);
Load = ReactiveCommand.Create...(..., canExecute: canRun);
Scan = ReactiveCommand.Create...(..., canExecute: canRun);
Observable.Merge(Run.IsExecuting, Load.IsExecuting, Scan.IsExecuting)
.Select(executing => !executing).Subscribe(canRun);

How to filter one observable with another observable?

With https://github.com/neuecc/UniRx,
I have two observables A and B.
I want A to be filtered by B. Sample seems like what I want but the negative of it.
IObservable<long> A = Observable.EveryUpdate();
IObservable<Collider2D> B = this.OnTriggerEnter2DAsObservable()
.Where( x => x.gameObject.tag == "Wall");
I want some kind of Pseudo code like that:
A.filterBy(B)
.Subscribe(x => Debug.Log(x)); //executed only when B is not streaming
(Update1)
Here is actual code. I am trying to cancel out input stream with colliding stream.
var isCollidingWithWall = this.OnTriggerEnter2DAsObservable()
.Where(collider => collider.gameObject.tag == "Wall");
Func<long, float> displaceCalculate = (_) => this.Speed * Time.deltaTime;
var moveLeft = Observable.EveryUpdate()
.Where(_ => Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow));
var moveRight = Observable.EveryUpdate()
.Where(_ => Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow));
var movement1 = moveLeft
.Select(displaceCalculate)
.Select(f => -f);
var movement2 = moveRight
.Select(displaceCalculate);
movement2
.Merge(movement1)
.Subscribe(f =>
{
this.transform.position = new Vector2(this.transform.position.x + f, this.transform.position.y);
});
I think I might be going in wrong direction.
It is difficult to only combine operators.
The two streams are not synchronized.
When the OnNext message comes from stream B, how long shut off stream A?
Next stream B message? or Next stream A?
If you want to stop it only one frame, how about this?
void Start()
{
var isCollisionEntered = false;
this.OnCollisionEnter2DAsObservable()
.Where(x => x.gameObject.tag == "Wall")
.Subscribe(_ => isCollisionEntered = true);
this.LateUpdateAsObservable()
.Where(_ => isCollisionEntered)
.Subscribe(_ => isCollisionEntered = false);
this.UpdateAsObservable()
.Where(_ => !isCollisionEntered)
.Subscribe(_ => Debug.Log("Do here"));
}
And, I don't recommend Observable.EveryUpdate .It is necessary to manage lifetime.
I recommend using this.UpdateAsObservable (UniRx.Triggers) instead.
It automatically publishes OnCompleted message on the gameobject destroyed.
I just came up with another way.
var streamB = this.OnTriggerEnter2DAsObservable().AsUnitObservable();
this.UpdateAsObservable()
.TakeUntil(streamB)
.RepeatUntilDestroy(this)
.Subscribe(_ =>
{
Debug.Log(Time.frameCount);
});
Can you provide a little more context about the actual game behavior you are trying to implement?
My guess would be that there is some other approach to what you are trying to do, without having to rely on EveryUpdate (e.g. by using OnTriggerStay and/or OnTriggerExit).
Just giving a guess to what you mean by "negative" of the sample operator: you might want to have a look at pausable. You'd have to generate the proper boolean values though, and how to do that really depends on what game behavior you are actually trying to implement here.

RX PropertyChanged GroupBy deadlock

I am trying to use Reactive Extensions to throttle PropertyChanged notifications. There are examples of doing this using GroupBy, but with one Subscription created for each PropertyName.
I want to handle the PropertyChanged event for all properties, and I need to Throttle those events for each PropertyName.
This is what I have so far, but it causes a deadlock.
ValuesPropertyChanged = Observable.FromEventPattern<PropertyChangedEventArgs>(value, "PropertyChanged")
.GroupBy(o => o.EventArgs.PropertyName)
.First()
.Throttle(TimeSpan.FromSeconds(2))
.Subscribe(args => HandlePropertyChanged(args.EventArgs.PropertyName));
The deadlock happens in the call to .First().
It still locks if I change that line to:
.Select(o => o.First())
I have also tried
.Select(o => o.FirstAsync())
The examples for GroupBy here look pretty concise, but I am incapable of wrapping my head around converting these examples to my solution.
Why does this cause a deadlock, and what should I do to make this work?
I think this might be what you're after:
// assume MyObj : INotifyPropertyChanged, naturally
var value = new MyObj();
Action<string> HandlePropertyChanged =
name => Console.WriteLine("Got a change for name:" + name);
// The query
var valuesPropertyChanged =
// create from event stream
from propChange in Observable.FromEventPattern<PropertyChangedEventArgs>(
value,
"PropertyChanged")
// group events by property name
group propChange by propChange.EventArgs.PropertyName into batchByName
// Throttle the resulting batch
from throttledByName in batchByName.Throttle(TimeSpan.FromSeconds(1))
// then select each item of the "throttled output"
select throttledByName;
valuesPropertyChanged.Subscribe(args =>
HandlePropertyChanged(args.EventArgs.PropertyName));
for(int i=0;i<10;i++)
{
value.Value1 = i.ToString();
value.Value2 = (i-1).ToString();
}
Output:
Got a change for name:Value2
Got a change for name:Value1
Here is the same but with extension methods:
var valuesPropertyChanged =
Observable.FromEventPattern<PropertyChangedEventArgs>(
_vm,
"PropertyChanged")
.GroupBy(propchange => propchange.EventArgs.PropertyName)
.Select(o => o.Throttle(TimeSpan.FromSeconds(1)))
.Merge();