Skip to content

Commit 860116b

Browse files
committed
Added property CurrentGroup
1 parent a576461 commit 860116b

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

KPCLib.xunit/PxDatabaseTests.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public void DeleteGroupTests(bool permanent)
159159
[InlineData("General/G1/G21/G21E1")]
160160
[InlineData("General/G1/G21/")]
161161
[InlineData("General/G1/G21")]
162+
[InlineData("/G1/G21")]
162163
/// <summary>
163164
/// Find group or entry test.
164165
/// </summary>
@@ -171,7 +172,8 @@ public void FindByPathTests(string path)
171172
}
172173
else
173174
{
174-
Debug.WriteLine($"{passxyz.PxDb.FindByPath<PwEntry>(path)}");
175+
if(path.StartsWith("/")) { Assert.Null(passxyz.PxDb.FindByPath<PwGroup>(path)); }
176+
else Debug.WriteLine($"{passxyz.PxDb.FindByPath<PwEntry>(path)}");
175177
}
176178
}
177179

@@ -189,21 +191,40 @@ public void FindEntryByPathTests(string path)
189191

190192
[Theory]
191193
[InlineData("General/G1/G21/G21E1")]
194+
[InlineData("/utdb/General/G1/G21")]
195+
[InlineData("../..")]
196+
[InlineData("..")]
192197
/// <summary>
193198
/// Find group using an entry pass.
194199
/// </summary>
195200
public void FindGroupByPathTests(string path)
196201
{
197-
passxyz.PxDb.CurrentGroup = passxyz.PxDb.RootGroup;
198-
Debug.WriteLine($"{passxyz.PxDb.FindByPath<PwGroup>(path)}");
199-
Assert.Null(passxyz.PxDb.FindByPath<PwGroup>(path));
202+
PwGroup group;
203+
204+
if (path.StartsWith("/")) {
205+
group = passxyz.PxDb.FindByPath<PwGroup>(path);
206+
Assert.NotNull(group);
207+
}
208+
else if(path.StartsWith(".."))
209+
{
210+
passxyz.PxDb.CurrentGroup = passxyz.PxDb.FindByPath<PwGroup>("/utdb/General/G1/G21");
211+
Debug.WriteLine($"Current group is: {passxyz.PxDb.CurrentGroup}");
212+
group = passxyz.PxDb.FindByPath<PwGroup>(path);
213+
}
214+
else
215+
{
216+
passxyz.PxDb.CurrentGroup = passxyz.PxDb.RootGroup;
217+
group = passxyz.PxDb.FindByPath<PwGroup>(path);
218+
Debug.WriteLine($"Cannot find group {path}");
219+
Assert.Null(group);
220+
}
200221
}
201222

202223
[Fact]
203224
public void FindByPathDefaultTests()
204225
{
205226
Assert.Null(passxyz.PxDb.FindByPath<PwEntry>());
206-
Assert.Equal(passxyz.PxDb.FindByPath<PwGroup>().ToString(), passxyz.PxDb.CurrentGroup.ToString());
227+
Assert.Equal(passxyz.PxDb.FindByPath<PwGroup>().ToString(), passxyz.PxDb.RootGroup.ToString());
207228
}
208229

209230
[Fact]
@@ -212,6 +233,13 @@ public void CurrentGroupTests()
212233
Debug.WriteLine($"{passxyz.PxDb.CurrentGroup}");
213234
Assert.NotNull(passxyz.PxDb.CurrentGroup);
214235
}
236+
237+
[Fact]
238+
public void CurrentPathTests()
239+
{
240+
Debug.WriteLine($"Current path is {passxyz.PxDb.CurrentPath}.");
241+
Assert.NotNull(passxyz.PxDb.CurrentPath);
242+
}
215243
}
216244

217245
public class PxLibInfoTests

PassXYZLib/PxDatabase.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ public PwGroup CurrentGroup
164164
set { this.LastSelectedGroup = value.Uuid; }
165165
}
166166

167+
public string CurrentPath
168+
{
169+
get {
170+
var group = this.CurrentGroup;
171+
string path = this.CurrentGroup.Name + "/";
172+
173+
while (this.RootGroup.Uuid != group.Uuid)
174+
{
175+
group = group.ParentGroup;
176+
path = group.Name + "/" + path;
177+
}
178+
179+
return path;
180+
}
181+
}
182+
167183
private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
168184
{
169185
if (pgRecycleBin == this.RootGroup)
@@ -195,8 +211,27 @@ private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
195211
if (this.IsOpen)
196212
{
197213
if (path == null) throw new ArgumentNullException("path");
214+
198215
string[] paths = path.Split('/');
199216
var lastSelectedGroup = this.CurrentGroup;
217+
218+
if (path.StartsWith("/"))
219+
{
220+
//
221+
// if the path start with "/", we have to remove "/root" and
222+
// search from the root group
223+
//
224+
if(path.StartsWith("/" + this.RootGroup.Name))
225+
{
226+
lastSelectedGroup = this.RootGroup;
227+
paths = String.Join("/", paths, 2, paths.Length - 2).Split('/');
228+
}
229+
else
230+
{
231+
return default(T);
232+
}
233+
}
234+
200235
if(paths.Length > 0)
201236
{
202237
if (typeof(T).Name == "PwGroup")
@@ -263,6 +298,12 @@ PwGroup FindSubgroup(PwGroup group, string name)
263298
if (group == null) throw new ArgumentNullException("group");
264299
if (name == null) throw new ArgumentNullException("name");
265300

301+
if(name == "..")
302+
{
303+
if (this.RootGroup.Uuid != group.Uuid) { return group.ParentGroup; }
304+
else { return null; }
305+
}
306+
266307
foreach (var gp in group.Groups)
267308
{
268309
if(gp.Name == name)

0 commit comments

Comments
 (0)