Skip to content

Commit

Permalink
Should be able to get the reason directly in the utils.
Browse files Browse the repository at this point in the history
Also, refactor the test case.
  • Loading branch information
andy840119 committed Sep 5, 2022
1 parent 91ceb78 commit dc005b2
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,50 @@ public void TestIsWriteLyricPropertyLocked()
});

void test(Lyric lyric)
=> testEveryWritablePropertyInObject<Lyric, Lyric>(lyric, (l, propertyName) => HitObjectWritableUtils.IsWriteLyricPropertyLocked(l, propertyName));
{
testEveryWritablePropertiesInObjectAtTheSameTime(lyric, (l, propertyName) => HitObjectWritableUtils.IsWriteLyricPropertyLocked(l, propertyName));
testEveryWritablePropertiesInObject(lyric, (l, propertyName) => HitObjectWritableUtils.IsWriteLyricPropertyLocked(l, propertyName));

testEveryWritablePropertiesInObjectAtTheSameTime(lyric, (l, propertyName) => HitObjectWritableUtils.GetLyricPropertyLockedReason(l, propertyName));
testEveryWritablePropertiesInObject(lyric, (l, propertyName) => HitObjectWritableUtils.GetLyricPropertyLockedReason(l, propertyName));
}
}

#endregion

#region Create or remove notes.

[Test]
public void TestGetLyricPropertyLockedReason()
public void TestIsCreateOrRemoveNoteLocked()
{
// standard.
test(new Lyric());

// test lock state.
foreach (var lockState in EnumUtils.GetValues<LockState>())
{
test(new Lyric
{
Lock = lockState
});
}

// reference lyric.
test(new Lyric
{
ReferenceLyricConfig = new ReferenceLyricConfig(),
});

test(new Lyric
{
ReferenceLyricConfig = new SyncLyricConfig(),
});

void test(Lyric lyric)
=> testEveryWritablePropertyInObject<Lyric, Lyric>(lyric, (l, propertyName) => HitObjectWritableUtils.GetLyricPropertyLockedReason(l, propertyName));
{
HitObjectWritableUtils.IsCreateOrRemoveNoteLocked(lyric);
HitObjectWritableUtils.GetCreateOrRemoveNoteLockedReason(lyric);
}
}

#endregion
Expand All @@ -74,35 +107,38 @@ public void TestIsWriteNotePropertyLocked()
});

void test(Note note)
=> testEveryWritablePropertyInObject<Note, Note>(note, (l, propertyName) => HitObjectWritableUtils.IsWriteNotePropertyLocked(l, propertyName));
}

[Test]
public void TestGetNotePropertyLockedReason()
{
// standard.
test(new Note());
{
testEveryWritablePropertiesInObjectAtTheSameTime(note, (l, propertyName) => HitObjectWritableUtils.IsWriteNotePropertyLocked(l, propertyName));
testEveryWritablePropertiesInObject(note, (l, propertyName) => HitObjectWritableUtils.IsWriteNotePropertyLocked(l, propertyName));

void test(Note note)
=> testEveryWritablePropertyInObject<Note, Note>(note, (l, propertyName) => HitObjectWritableUtils.GetNotePropertyLockedReason(l, propertyName));
testEveryWritablePropertiesInObjectAtTheSameTime(note, (l, propertyName) => HitObjectWritableUtils.GetNotePropertyLockedReason(l, propertyName));
testEveryWritablePropertiesInObject(note, (l, propertyName) => HitObjectWritableUtils.GetNotePropertyLockedReason(l, propertyName));
}
}

#endregion

private void testEveryWritablePropertyInObject<THitObject, TProperty>(TProperty property, Action<TProperty, string> action)
private static void testEveryWritablePropertiesInObjectAtTheSameTime<THitObject>(THitObject hitObject, Action<THitObject, string[]> action)
{
// the purpose of this test case if focus on checking every property in the hit-object should be able to know the writable or not.
// return value is not in the test scope.
string[] allWriteableProperties = typeof(THitObject).GetProperties()
.Where(x => x.CanRead && x.CanWrite)
.Where(x => x.CustomAttributes.All(customAttributeData => customAttributeData.AttributeType != typeof(JsonIgnoreAttribute)))
.Select(x => x.Name)
.ToArray();
action(hitObject, allWriteableProperties);
}

var allWriteableProperties = typeof(THitObject).GetProperties().Where(x => x.CanRead && x.CanWrite);

foreach (var propertyInfo in allWriteableProperties)
private static void testEveryWritablePropertiesInObject<THitObject>(THitObject hitObject, Action<THitObject, string> action)
{
testEveryWritablePropertiesInObjectAtTheSameTime(hitObject, (l, propertyNames) =>
{
if (propertyInfo.CustomAttributes.Any(x => x.AttributeType == typeof(JsonIgnoreAttribute)))
continue;

action(property, propertyInfo.Name);
}
foreach (string propertyName in propertyNames)
{
action(hitObject, propertyName);
}
});
}
}
}
83 changes: 72 additions & 11 deletions osu.Game.Rulesets.Karaoke/Edit/Utils/HitObjectWritableUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq;
using osu.Framework.Localisation;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Properties;
using osu.Game.Rulesets.Karaoke.Objects.Types;
Expand All @@ -14,14 +15,31 @@ public static class HitObjectWritableUtils
#region Lyric property

public static bool IsWriteLyricPropertyLocked(Lyric lyric, params string[] propertyNames)
=> propertyNames.All(x => IsWriteLyricPropertyLocked(lyric, x));
=> getLyricPropertyLockedBy(lyric, propertyNames) != null;

public static bool IsWriteLyricPropertyLocked(Lyric lyric, string propertyName)
=> GetLyricPropertyLockedReason(lyric, propertyName) != null;
=> getLyricPropertyLockedBy(lyric, propertyName) != null;

public static LockLyricPropertyBy? GetLyricPropertyLockedReason(Lyric lyric, params string[] propertyNames)
public static LocalisableString? GetLyricPropertyLockedReason(Lyric lyric, params string[] propertyNames)
=> getLyricPropertyLockedMessage(getLyricPropertyLockedBy(lyric, propertyNames));

public static LocalisableString? GetLyricPropertyLockedReason(Lyric lyric, string propertyName)
=> getLyricPropertyLockedMessage(getLyricPropertyLockedBy(lyric, propertyName));

private static LocalisableString? getLyricPropertyLockedMessage(LockLyricPropertyBy? reasons)
{
return reasons switch
{
LockLyricPropertyBy.ReferenceLyricConfig => "Cannot modify this property due to this lyric is property is sync from another lyric.",
LockLyricPropertyBy.LockState => "This property is locked and not editable",
null => default(LocalisableString?),
_ => throw new ArgumentOutOfRangeException()
};
}

private static LockLyricPropertyBy? getLyricPropertyLockedBy(Lyric lyric, params string[] propertyNames)
{
var reasons = propertyNames.Select(x => GetLyricPropertyLockedReason(lyric, x))
var reasons = propertyNames.Select(x => getLyricPropertyLockedBy(lyric, x))
.Where(x => x != null)
.OfType<LockLyricPropertyBy>()
.ToArray();
Expand All @@ -35,7 +53,7 @@ public static bool IsWriteLyricPropertyLocked(Lyric lyric, string propertyName)
return null;
}

public static LockLyricPropertyBy? GetLyricPropertyLockedReason(Lyric lyric, string propertyName)
private static LockLyricPropertyBy? getLyricPropertyLockedBy(Lyric lyric, string propertyName)
{
bool lockedByConfig = isWriteLyricPropertyLockedByConfig(lyric.ReferenceLyricConfig, propertyName);
if (lockedByConfig)
Expand Down Expand Up @@ -111,7 +129,21 @@ private static bool isWriteLyricPropertyLockedByConfig(IReferenceLyricPropertyCo
public static bool IsCreateOrRemoveNoteLocked(Lyric lyric)
=> GetCreateOrRemoveNoteLockedReason(lyric) != null;

public static LockLyricPropertyBy? GetCreateOrRemoveNoteLockedReason(Lyric lyric)
public static LocalisableString? GetCreateOrRemoveNoteLockedReason(Lyric lyric)
=> getCreateOrRemoveNoteLockedMessage(getCreateOrRemoveNoteLockedBy(lyric));

private static LocalisableString? getCreateOrRemoveNoteLockedMessage(LockLyricPropertyBy? reasons)
{
return reasons switch
{
LockLyricPropertyBy.ReferenceLyricConfig => "Cannot modify this property due to this lyric is property is sync from another lyric.",
LockLyricPropertyBy.LockState => "This property is locked and not editable",
null => default(LocalisableString?),
_ => throw new ArgumentOutOfRangeException()
};
}

private static LockLyricPropertyBy? getCreateOrRemoveNoteLockedBy(Lyric lyric, params string[] propertyNames)
{
bool lockedByConfig = isCreateOrRemoveNoteLocked(lyric.ReferenceLyricConfig);
if (lockedByConfig)
Expand All @@ -137,12 +169,41 @@ private static bool isCreateOrRemoveNoteLocked(IReferenceLyricPropertyConfig? co
#region Note property

public static bool IsWriteNotePropertyLocked(Note note, params string[] propertyNames)
=> propertyNames.All(x => IsWriteNotePropertyLocked(note, x));
=> getNotePropertyLockedBy(note, propertyNames) != null;

public static bool IsWriteNotePropertyLocked(Note note, string propertyName)
=> GetNotePropertyLockedReason(note, propertyName) != null;
=> getNotePropertyLockedBy(note, propertyName) != null;

public static LocalisableString? GetNotePropertyLockedReason(Note note, params string[] propertyNames)
=> getNotePropertyLockedMessage(getNotePropertyLockedBy(note, propertyNames));

public static LocalisableString? GetNotePropertyLockedReason(Note note, string propertyName)
=> getNotePropertyLockedMessage(getNotePropertyLockedBy(note, propertyName));

private static LocalisableString? getNotePropertyLockedMessage(LockNotePropertyBy? reasons)
{
return reasons switch
{
LockNotePropertyBy.ReferenceLyricConfig => "Cannot modify this property due to this note's lyric is property is sync from another lyric.",
null => default(LocalisableString?),
_ => throw new ArgumentOutOfRangeException()
};
}

private static LockNotePropertyBy? getNotePropertyLockedBy(Note note, params string[] propertyNames)
{
var reasons = propertyNames.Select(x => getNotePropertyLockedBy(note, x))
.Where(x => x != null)
.OfType<LockNotePropertyBy>()
.ToArray();

if (reasons.Contains(LockNotePropertyBy.ReferenceLyricConfig))
return LockNotePropertyBy.ReferenceLyricConfig;

return null;
}

public static LockNotePropertyBy? GetNotePropertyLockedReason(Note note, string propertyName)
private static LockNotePropertyBy? getNotePropertyLockedBy(Note note, string propertyName)
{
var lyric = note.ReferenceLyric;

Expand All @@ -161,14 +222,14 @@ private static bool isWriteNotePropertyLockedByReferenceLyric(Lyric lyric, strin

#endregion

public enum LockLyricPropertyBy
private enum LockLyricPropertyBy
{
ReferenceLyricConfig,

LockState,
}

public enum LockNotePropertyBy
private enum LockNotePropertyBy
{
ReferenceLyricConfig,
}
Expand Down

0 comments on commit dc005b2

Please sign in to comment.