how to compare subset collection by value (structural equiality) with FluentAssertions? - fluent-assertions

I want to assert if collection A is subset of collection B, using structural equality comparison (like BeEquivalentTo), using FluentAssetion. Example:
var p1 = new Point(1,2)
var p1_1 = new Point(1,2)
var p2 = new Point(2,3)
var p3 = new Point(4,5)
var A = new [] { p1 }
var B = new [] { p2, p1_1}
var C = new [] { p2, p3}
A.Should().SubsetBeEquivalentTo(B) // The assert should success
A.Should().SubsetBeEquivalentTo(C) // The assertion should fail
So, is there a way to get the behavior showed for SubsetBeEquivalentTo in the FluentAssertion library?

Unfortunately, ContainEquivalentOf only accepts a single item. But it's a good suggestion. Maybe you can suggest that here.

Related

How to parse subfolders for find specific files?

I Would like to create a script for photoshop who allow me to search some files with a specific name (for example : 300x250_F1.jpg, 300x250_F2.jpg, 300x600_F1.jpg, etc... ) in differents subfolders (all in the same parent folder) and after load them in my active document. The problem is names of subfolders will be everytime differents.
I definitely need some help :)
i found a code which almost do what i want (thank you).
I'm almost good but i have a problem: if the variable "mask" have only one value, it works. But with few values, it doesn't work anymore.
I think it's because i made an array with the mask variable and i have to update the script...
var topFolder = Folder.selectDialog("");
//var topFolder = new Folder('~/Desktop/PS_TEST');
var fileandfolderAr = scanSubFolders(topFolder, /\.(jpg)$/i);
//var fileandfolderAr = scanSubFolders(topFolder, /\.(jpg|tif|psd|bmp|gif|png|)$/i);
var fileList = fileandfolderAr[0];
var nom = decodeURI(fileList);
//all file paths found and amount of files found
//alert("fileList: " + nom + "\n\nFile Amount: " + fileList.length);
//alert(allFiles);
for (var a = 0; a < fileList.length; a++) {
var docRef = open(fileList[a]);
//do things here
}
function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
var sFolders = [];
var allFiles = [];
var mask = ["300x250_F1", "300x250_F2"];
sFolders[0] = tFolder;
for (var j = 0; j < sFolders.length; j++) { // loop through folders
var procFiles = sFolders[j].getFiles();
for (var i = 0; i < procFiles.length; i++) { // loop through this folder contents
if (procFiles[i] instanceof File) {
if (mask == undefined) {
allFiles.push(procFiles); // if no search mask collect all files
}
if (procFiles[i].fullName.search(mask) != -1) {
allFiles.push(procFiles[i]); // otherwise only those that match mask
}
}
else if (procFiles[i] instanceof Folder) {
sFolders.push(procFiles[i]); // store the subfolder
scanSubFolders(procFiles[i], mask); // search the subfolder
}
}
}
return [allFiles, sFolders];
}
There are several ways you can accomplish this without reassigning mask within the scanSubFolders function.
Solution 1: use a regex
The function is already set up to accept a regex or string as a mask. You just need to use one that would match the pattern of the files you're targeting.
var fileandfolderAr = scanSubFolders(topFolder, /300x250_F(1|2)/gi);
Solution 2: call the function within a loop
If regex isn't your thing, you could still utilize an array of strings, but do it outside the function. Loop the array of masks and call the function with each one, then execute your primary logic on the results of each call.
var topFolder = Folder.selectDialog("");
var myMasks = ["300x250_F1", "300x250_F2"];
for (var index in myMasks) {
var mask = myMasks[index]
var fileandfolderAr = scanSubFolders(topFolder, mask);
var fileList = fileandfolderAr[0];
for (var a = 0; a < fileList.length; a++) {
var docRef = open(fileList[a]);
//do things here
}
}
Don't forget to remove var mask = ["300x250_F1", "300x250_F2"]; from within the scanSubFolders function or else these won't work.

wait for Future in for loop

I'm having a future which is a result of an sql query, in it I'm looping over each returned row to add it to a list with a map to encode it to a json format later.
In this loop I'm executing another query depending on the result in each row of the outer query and add those rows again to a map.
Future<Results> mysqlAllTags = mysqlCon.query(query).then((results){
// Make new list as a part of the JsonObject
json.tags = new List();
// Loop throught the row of the result from Mysql data
return results.forEach((row){
// Create Map to put the word, value, rating and id into the JsonObject
Map data = new Map();
// Put the Mysql data into the Map
data["word"] = row.word.toString();
data["value"] = row.value.toString();
data["rating"] = row.rating.toString();
data["id_tag"] = row.id_tag.toString();
data["replacing"] = null;
// Add the Map to the userPages list
json.tags.add(data);
}).then((e){
for(var tag in json.tags){
//Map dataReplacing = getReplacing(userId, row.id_tag.toString());
String replacingTags = getReplacingSQL(tag['id_tag'].toString());
mysqlCon.query(replacingTags).then((result){
result.forEach((row1){
Map map = new Map();
map["word"] = row1.word.toString();
map["value"] = row1.value.toString();
map["id_tag"] = row1.id_replacing_tag.toString();
tag["replacing"] = map;
}).then((e){
print("then inner for called");
return null;
});
print("then inner for called");
return null;
});
}
print("outer for returned");
// Send the data in UTF8 to the client
result = Helpers.formatJsonAndEncodeUtf8('OK', session:_session, data: [json]);
return null;
}).catchError((error){
result = Helpers.formatJsonAndEncodeUtf8('ERROR 856284555 (Could not load tags)', session:_session);
});
}).catchError((error){
result = Helpers.formatJsonAndEncodeUtf8('ERROR 2346644555 (Could not load tags)', session:_session);
});
return Future.wait([mysqlAllTags]).then((e){
print("future returned");
return result;
});
The result looks like this:
outer for returned
=== TO CLIENT ===
{"status":[{"message":"OK","csrfToken":"99"}],"data":[{"tags":[{"word":"Melon","value":"11.0","rating":"1","id_tag":"37","replacing":null},........}]}
=================
future returned
then inner for called
then inner for called
then inner for called
then inner for called
then inner for called
then inner for called
then inner for called
then inner for called
How can I wait until all Futures in my for loop are finished?
I solved it by adding the result declaration in the Future.wait(futures).then() function. Thank you Günter Zöchbauer for your input.
Future<Results> mysqlAllTags = mysqlCon.query(query).then((results){
// Make new list as a part of the JsonObject
json.tags = new List();
// Loop throught the row of the result from Mysql data
return results.forEach((row){
// Create Map to put the word, value, rating and id into the JsonObject
Map data = new Map();
// Put the Mysql data into the Map
data["word"] = row.word.toString();
data["value"] = row.value.toString();
data["rating"] = row.rating.toString();
data["id_tag"] = row.id_tag.toString();
data["replacing"] = null;
// Add the Map to the userPages list
json.tags.add(data);
}).then((e){
var futures = []; // added
for(var tag in json.tags){
print("row ");
print(json.tags);
//Map dataReplacing = getReplacing(userId, row.id_tag.toString());
String replacingTags = getReplacingSQL(tag['id_tag'].toString());
// added `futures.add(...)`
futures.add(mysqlCon.query(replacingTags).then((result) {
result.forEach((row1){
Map map = new Map();
map["word"] = row1.word.toString();
map["value"] = row1.value.toString();
map["id_tag"] = row1.id_replacing_tag.toString();
tag["replacing"] = map;
}).then((e){
print("then inner for called");
return null;
});
print("then inner for called");
return null;
}));
}
print("outer for returned");
// Send the data in UTF8 to the client
return Future.wait(futures).then((e){
result = Helpers.formatJsonAndEncodeUtf8('OK', session:_session, data: [json]);
}); // added
}).catchError((error){
result = Helpers.formatJsonAndEncodeUtf8('ERROR 856284555 (Could not load tags)', session:_session);
});
}).catchError((error){
result = Helpers.formatJsonAndEncodeUtf8('ERROR 2346644555 (Could not load tags)', session:_session);
});
return Future.wait([mysqlAllTags]).then((e){
print("future returned");
return result;
});
I'm not sure if this fixes all issues, it's not easy to see in such code what exactly returns a future. (I added comments where I added something)
Future<Results> mysqlAllTags = mysqlCon.query(query).then((results){
// Make new list as a part of the JsonObject
json.tags = new List();
// Loop throught the row of the result from Mysql data
return results.forEach((row){
// Create Map to put the word, value, rating and id into the JsonObject
Map data = new Map();
// Put the Mysql data into the Map
data["word"] = row.word.toString();
data["value"] = row.value.toString();
data["rating"] = row.rating.toString();
data["id_tag"] = row.id_tag.toString();
data["replacing"] = null;
// Add the Map to the userPages list
json.tags.add(data);
}).then((e){
var futures = []; // added
for(var tag in json.tags){
print("row ");
print(json.tags);
//Map dataReplacing = getReplacing(userId, row.id_tag.toString());
String replacingTags = getReplacingSQL(tag['id_tag'].toString());
// added `futures.add(...)`
futures.add(mysqlCon.query(replacingTags).then((result) {
result.forEach((row1){
Map map = new Map();
map["word"] = row1.word.toString();
map["value"] = row1.value.toString();
map["id_tag"] = row1.id_replacing_tag.toString();
tag["replacing"] = map;
}).then((e){
print("then inner for called");
return null;
});
print("then inner for called");
return null;
}));
}
print("outer for returned");
// Send the data in UTF8 to the client
result = Helpers.formatJsonAndEncodeUtf8('OK', session:_session, data: [json]);
return Future.wait(futures); // added
}).catchError((error){
result = Helpers.formatJsonAndEncodeUtf8('ERROR 856284555 (Could not load tags)', session:_session);
});
}).catchError((error){
result = Helpers.formatJsonAndEncodeUtf8('ERROR 2346644555 (Could not load tags)', session:_session);
});
return Future.wait([mysqlAllTags]).then((e){
print("future returned");
return result;
});

get list of decision for a specific meetingtitle using linq asp.net

I have a database table. What I want is to get data using group by clause as I have used in below code.
Note that Decision is another table. now I want that all the decisions related to a specific Meeting Title should be shown in list.like
meetingtitle1=decision1,decison2,decison3
meetingtitle2=decision1,decison2
but below code returns only one decisiontitle.
public List<NewMeetings> GetAllMeetings()
{
var xyz = (from m in DB.MeetingAgenda
//join mp in Meeting on m.MeetingId equals mp.MeetingId
//where m.MeetingId == 2
group m by new { m.Meeting.MeetingTitle } into grp
select new NewMeetings
{
// meetingid = grp.Key.MeetingId,
meetingtitle = grp.Key.MeetingTitle,
decision = grp.Select(x => x.Decision.DecisionTitle).FirstOrDefault(),
total = grp.Count()
}).ToList();
List<NewMeetings> list = xyz.ToList();
return list;
}
public class NewMeetings
{
public int meetingid;
public string meetingtitle;
public string decision;
public int total;
}
Can somebody please tell me how to return a list of decisions to a specific Meetingtitle?
You are doing a FirstOrDefault on the list of decisions which obviously means you are only getting a single value. Instead you can join them all together into one longer string (separated by commas as you indicated in the question) by changing this line:
decision = grp.Select(x => x.Decision.DecisionTitle).FirstOrDefault(),
To this:
decision = string.Join(",", grp.Select(x => x.Decision.DecisionTitle)),
However, as the string.Join is not recognised by Linq to Entities, you need to do the string.Join after the data has been retrieved (i.e. after the ToList):
var xyz = (from m in DB.MeetingAgenda
group m by new { m.Meeting.MeetingTitle } into grp
select new
{
meetingtitle = grp.Key.MeetingTitle,
decisions = grp.Select(x => x.Decision.DecisionTitle),
total = grp.Count()
})
.ToList()
.Select(m => new NewMeetings
{
meetingtitle = m.meetingtitle,
decision = string.Join(",", m.decisions),
total = m.total
});

How do I programmatically capture and replicate Ai path shape?

I'm using ExtendScript for scripting Adobe Illustrator. I was wondering if there was a sneaky way or a script available to programmatically capture and then replicate a path shape, sort of JavaScript's .toSource() equivalent.
Thanks
Try this:
main();
function main(){
var doc = app.activeDocument; // get the active doc
var coords = new Array(); // make a new array for the coords of the path
var directions = new Array();
var sel = doc.selection[0];// get first object in selection
if(sel == null) {
// check if something is slected
alert ("You need to sevlect a path");
return;
}
var points = sel.pathPoints;// isolate pathpoints
// loop points
for (var i = 0; i < points.length; i++) {
// this could be done in one lines
// just to see whats going on line like
//~ coords.push(new Array(points[i].anchor[0],points[i].anchor[1]));
var p = points[i]; // the point
var a = p.anchor; // his anchor
var px = a[0];// x
var py = a[1]; // y
var ldir = p.leftDirection;
var rdir = p.rightDirection;
directions.push(new Array(ldir,rdir));
coords.push(new Array(px,py));// push into new array of array
}
var new_path = doc.pathItems.add(); // add a new pathitem
new_path.setEntirePath(coords);// now build the path
// check if path was closed
if(sel.closed){
new_path.closed = true;
}
// set the left and right directions
for(var j = 0; j < new_path.pathPoints.length;j++){
new_path.pathPoints[j].leftDirection = directions[j][0];
new_path.pathPoints[j].rightDirection = directions[j][1];
}
}

JScript.NET private variables

I'm wondering about JScript.NET private variables. Please take a look on the following code:
import System;
import System.Windows.Forms;
import System.Drawing;
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
}
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
}
var addHeader = function(){
out('header');
}
return {
endDocument: function(){
state = 1;
addHeader();
out('endDocument');
},
beginDocument: function(){
beginPage();
}
}
}
var j = new jsPDF();
j.beginDocument();
j.endDocument();
Output:
beginPage 2
header 2
endDocument 2
if I run the same script in any browser, the output is:
beginPage 2
header 1
endDocument 1
Why it is so??
Thanks,
Paul.
Just a guess, but it appears that JScript.NET doesn't support closures the same way as EMCAScript, so the state variable in endDocument() isn't referencing the private member of the outer function, but rather an local variable (undeclared). Odd.
You don't have to use new when calling jsPDF here since you're using a singleton pattern. jsPDF is returning an object literal so even without new you'll have access to the beginPage and endDocument methods. To be perfectly honest I don't know what the specifications call for when using new on a function that returns an object literal so I'm not sure if JScript.NET is getting it wrong or the browser. But for now try either getting rid of the new before jsPDF() or change your function to this:
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
};
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
};
var addHeader = function(){
out('header');
};
this.endDocument = function(){
state = 1;
addHeader();
out('endDocument');
};
this.beginDocument: function(){
beginPage();
};
}
That will allow you to use the new keyword and create more than one jsPDF object.
I've come across the same problem. In the following code, the closure bound to fun should contain only one variable called result. As the code stands, the variable result in the function with one parameter seems to be different to the result variable in the closure.
If in this function the line
result = [];
is removed, then the result in the line
return result;
refers to the result in the closure.
var fun = function() {
var result = [];
// recursive descent, collects property names of obj
// dummy parameter does nothing
var funAux = function(obj, pathToObj, dummy) {
if (typeof obj === "object") {
for (var propName in obj) {
if (obj.hasOwnProperty(propName)) {
funAux(obj[propName], pathToObj.concat(propName), dummy);
}
}
}
else {
// at leaf property, save path to leaf
result.push(pathToObj);
}
}
return function(obj) {
// remove line below and `result' 3 lines below is `result' in closure
result = []; // does not appear to be bound to `result' above
funAux(obj, [], "dummy");
return result; // if result 2 lines above is set, result is closure is a different variable
};
}();